Skip to content

Latest commit

 

History

History
345 lines (283 loc) · 8.69 KB

contour-plots.md

File metadata and controls

345 lines (283 loc) · 8.69 KB
jupyter
jupytextkernelspeclanguage_infoplotly
notebook_metadata_filtertext_representation
all
extensionformat_nameformat_versionjupytext_version
.md
markdown
1.1
1.1.1
display_namelanguagename
Python 3
python
python3
codemirror_modefile_extensionmimetypenamenbconvert_exporterpygments_lexerversion
nameversion
ipython
3
.py
text/x-python
python
python
ipython3
3.6.7
descriptiondisplay_aslanguagelayoutnameorderpage_typepermalinkthumbnail
How to make Contour plots in Python with Plotly.
scientific
python
base
Contour Plots
1
example_index
python/contour-plots/
thumbnail/contour.jpg

Basic Contour Plot

A 2D contour plot shows the contour lines of a 2D numerical array z, i.e. interpolated lines of isovalues of z.

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]] )) fig.show()

Setting X and Y Coordinates in a Contour Plot

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], x=[-9, -6, -5 , -3, -1], # horizontal axisy=[0, 1, 4, 5, 7] # vertical axis )) fig.show()

Colorscale for Contour Plot

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorscale='Electric', )) fig.show()

Customizing Size and Range of a Contour Plot's Contours

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorscale='Hot', contours=dict( start=0, end=8, size=2, ), )) fig.show()

Customizing Spacing Between X and Y Axis Ticks

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z= [[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], dx=10, x0=5, dy=10, y0=10, ) ) fig.show()

Connect the Gaps Between None Values in the Z Matrix

importplotly.graph_objsasgofromplotly.subplotsimportmake_subplotsfig=make_subplots(rows=2, cols=2, subplot_titles=('connectgaps = False', 'connectgaps = True')) z= [[None, None, None, 12, 13, 14, 15, 16], [None, 1, None, 11, None, None, None, 17], [None, 2, 6, 7, None, None, None, 18], [None, 3, None, 8, None, None, None, 19], [5, 4, 10, 9, None, None, None, 20], [None, None, None, 27, None, None, None, 21], [None, None, None, 26, 25, 24, 23, 22]] fig.add_trace(go.Contour(z=z, showscale=False), 1, 1) fig.add_trace(go.Contour(z=z, showscale=False, connectgaps=True), 1, 2) fig.add_trace(go.Heatmap(z=z, showscale=False, zsmooth='best'), 2, 1) fig.add_trace(go.Heatmap(z=z, showscale=False, connectgaps=True, zsmooth='best'), 2, 2) fig['layout']['yaxis1'].update(title=dict(text='Contour map')) fig['layout']['yaxis3'].update(title=dict(text='Heatmap')) fig.show()

Smoothing the Contour lines

importplotly.graph_objectsasgofromplotly.subplotsimportmake_subplotsimportnumpyasnpz= [[2, 4, 7, 12, 13, 14, 15, 16], [3, 1, 6, 11, 12, 13, 16, 17], [4, 2, 7, 7, 11, 14, 17, 18], [5, 3, 8, 8, 13, 15, 18, 19], [7, 4, 10, 9, 16, 18, 20, 19], [9, 10, 5, 27, 23, 21, 21, 21], [11, 14, 17, 26, 25, 24, 23, 22]] fig=make_subplots(rows=1, cols=2, subplot_titles=('Without Smoothing', 'With Smoothing')) fig.add_trace(go.Contour(z=z, line_smoothing=0), 1, 1) fig.add_trace(go.Contour(z=z, line_smoothing=0.85), 1, 2) fig.show()

Smooth Contour Coloring

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], # heatmap gradient coloring is applied between each contour levelcontours_coloring='heatmap'# can also be 'lines', or 'none' ) ) fig.show()

Contour Line Labels

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], contours=dict( coloring='heatmap', showlabels=True, # show labels on contourslabelfont=dict( # label font propertiessize=12, color='white', ) ))) fig.show()

Contour Lines

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], contours_coloring='lines', line_width=2, ) ) fig.show()

Custom Contour Plot Colorscale

importplotly.graph_objectsasgo# Valid color strings are CSS colors, rgb or hex stringscolorscale= [[0, 'gold'], [0.5, 'mediumturquoise'], [1, 'lightsalmon']] fig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorscale=colorscale) ) fig.show()

Color Bar Title

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorbar=dict( title=dict( text='Color bar title', # title hereside='right', font=dict( size=14, family='Arial, sans-serif') ) ), )) fig.show()

Color Bar Size for Contour Plots

In the example below, both the thickness (given here in pixels) and the length (given here as a fraction of the plot height) are set.

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorbar=dict( thickness=25, thicknessmode='pixels', len=0.6, lenmode='fraction', outlinewidth=0 ) )) fig.show()

Styling Color Bar Ticks for Contour Plots

importplotly.graph_objectsasgofig=go.Figure(data=go.Contour( z=[[10, 10.625, 12.5, 15.625, 20], [5.625, 6.25, 8.125, 11.25, 15.625], [2.5, 3.125, 5., 8.125, 12.5], [0.625, 1.25, 3.125, 6.25, 10.625], [0, 0.625, 2.5, 5.625, 10]], colorbar=dict(nticks=10, ticks='outside', ticklen=5, tickwidth=1, showticklabels=True, tickangle=0, tickfont_size=12) )) fig.show()

Reference

See https://plotly.com/python/reference/contour/ for more information and chart attribute options!

close