How to make mixed subplots in Python with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
In [1]:
import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd # read in volcano database data df = pd.read_csv( "https://raw.githubusercontent.com/plotly/datasets/master/volcano_db.csv", encoding="iso-8859-1", ) # frequency of Country freq = df['Country'].value_counts().reset_index() freq.columns = ['x', 'Country'] # read in 3d volcano surface data df_v = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv") # Initialize figure with subplots fig = 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 locations fig.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 chart fig.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 volcano fig.add_trace( go.Surface(z=df_v.values.tolist(), showscale=False), row=2, col=2 ) # Update geo subplot properties fig.update_geos( projection_type="orthographic", landcolor="white", oceancolor="MidnightBlue", showocean=True, lakecolor="LightBlue" ) # Rotate x-axis labels fig.update_xaxes(tickangle=45) # Set theme, margin, and annotation in layout fig.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()What About Dash?ΒΆ
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see fig.show()
, you can display the same figure in a Dash application by passing it to the figure
argument of the Graph
component from the built-in dash_core_components
package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4