plotly

Plotly.Express.Density_contour

“A density contour refers to a 2-dimensional histogram resembling a contour plot but is computed by grouping a set of data points as defined by x and y coordinates and applying an aggregate function.

Let’s discuss how to create a density contour plot using Plotly express.”

Function Syntax

The function syntax is as shown:

plotly.express.density_contour(data_frame=None, x=None, y=None, z=None, color=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, hover_name=None, hover_data=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, orientation=None, color_discrete_sequence=None, color_discrete_map=None, marginal_x=None, marginal_y=None, trendline=None, trendline_options=None, trendline_color_override=None, trendline_scope='trace', log_x=False, log_y=False, range_x=None, range_y=None, histfunc=None, histnorm=None, nbinsx=None, nbinsy=None, text_auto=False, title=None, template=None, width=None, height=None)

 
Function parameters:

    1. data_frame – specifies the data containing the columns used in the plot.
    2. x – defines the values used to position the marks along the x-axis.
    3. y – sets the values used to position the marks along the y axis.
    4. z – defines the values used to position the marks along the z-axis.
    5. color – defines the values used to assign a unique color to the marks of the plot.
    6. orientation – defines the orientation of the plot.
    7. title – sets the title for the figure.
    8. template – defines the plotly template used in the figure.
    9. width/height – sets the width and height of the figure in pixels.

Practical Example

The code below shows how to create a density contour plot using Plotly Express and the iris data.

import plotly.express as px
df = px.data.tips()
fig = px.density_contour(df, x='total_bill', y='tip')
fig.show()

 
Output:

Color Contours

To color the contours, you can specify the color parameter as shown in the example below:

import plotly.express as px
df = px.data.tips()
fig = px.density_contour(df, x='total_bill', y='tip', color='sex')
fig.show()

 
The resulting figure is shown below:

Facetted Density Contour

You can also create a facetted contour plot by setting the facet_col parameter as shown:

import plotly.express as px
df = px.data.tips()
fig = px.density_contour(df, x='total_bill', y='tip', facet_col='sex', color='smoker')
fig.show()

 
This should display a plot as shown:

Showing Labels

To display the labels in the plot, we can set the contours_showlabels.

fig = px.density_contour(df, x='total_bill', y='tip', facet_col='sex', color='smoker')
fig.update_traces(contours_showlabels=True)
fig.show()

 
The code above shows the return of the contour with the data labels included.

Continuously Colored Contour

If you wish to create a contour that is continuously colored, you can set he contours_coloring parameter to fill as:

fig = px.density_contour(df, x='total_bill', y='tip', facet_col='sex', color='smoker')
fig.update_traces(contours_showlabels=True, contours_coloring='fill')
fig.show()

 
The resulting figure is as shown:

Specifying Aggregate Function

You can also specify an aggregate function to the density contour by setting the z parameter and the histfunc parameters.

An example code is as shown:

import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x='petal_length', y='petal_width', z='sepal_length', histfunc='max')
fig.show()

 
The code above will return a density contour as shown:

Conclusion

This article explores the density_contour() function, which allows you to create 2d histogram plots.

Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list