Skip to content

Latest commit

 

History

History
126 lines (107 loc) · 3.54 KB

mixed-subplots.md

File metadata and controls

126 lines (107 loc) · 3.54 KB
jupyter
jupytextkernelspeclanguage_infoplotly
notebook_metadata_filtertext_representation
all
extensionformat_nameformat_versionjupytext_version
.md
markdown
1.2
1.4.2
display_namelanguagename
Python 3
python
python3
codemirror_modefile_extensionmimetypenamenbconvert_exporterpygments_lexerversion
nameversion
ipython
3
.py
text/x-python
python
python
ipython3
3.7.7
descriptiondisplay_aslanguagelayoutnameorderpage_typepermalinkthumbnail
How to make mixed subplots in Python with Plotly.
multiple_axes
python
base
Mixed Subplots
1
example_index
python/mixed-subplots/
thumbnail/mixed_subplot.JPG

Mixed Subplots and Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

Note: At this time, Plotly Express does not support creating figures with arbitrary mixed subplots i.e. figures with subplots of different types. Plotly Express only supports facet plots and marginal distribution subplots. To make a figure with mixed subplots, use the make_subplots() function in conjunction with graph objects as documented below.

Mixed Subplot

importplotly.graph_objectsasgofromplotly.subplotsimportmake_subplotsimportpandasaspd# read in volcano database datadf=pd.read_csv( "https://raw.githubusercontent.com/plotly/datasets/master/volcano_db.csv", encoding="iso-8859-1", ) # frequency of Countryfreq=df['Country'].value_counts().reset_index() freq.columns= ['x', 'Country'] # read in 3d volcano surface datadf_v=pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv") # Initialize figure with subplotsfig=make_subplots( rows=2, cols=2, column_widths=[0.6, 0.4], row_heights=[0.4, 0.6], specs=[[{"type": "scattergeo", "rowspan": 2}, {"type": "bar"}], [ None , {"type": "surface"}]]) # Add scattergeo globe map of volcano locationsfig.add_trace( go.Scattergeo(lat=df["Latitude"], lon=df["Longitude"], mode="markers", hoverinfo="text", showlegend=False, marker=dict(color="crimson", size=4, opacity=0.8)), row=1, col=1 ) # Add locations bar chartfig.add_trace( go.Bar(x=freq["x"][0:10],y=freq["Country"][0:10], marker=dict(color="crimson"), showlegend=False), row=1, col=2 ) # Add 3d surface of volcanofig.add_trace( go.Surface(z=df_v.values.tolist(), showscale=False), row=2, col=2 ) # Update geo subplot propertiesfig.update_geos( projection_type="orthographic", landcolor="white", oceancolor="MidnightBlue", showocean=True, lakecolor="LightBlue" ) # Rotate x-axis labelsfig.update_xaxes(tickangle=45) # Set theme, margin, and annotation in layoutfig.update_layout( template="plotly_dark", margin=dict(r=10, t=25, b=40, l=60), annotations=[ dict( text="Source: NOAA", showarrow=False, xref="paper", yref="paper", x=0, y=0) ] ) fig.show()

Reference

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

close