How to customize hover-template on with what information to show

For Plotly Express, you need to use the custom_data argument when you create the figure. For example: fig = px.scatter( data_frame=df, x=’ColX’, y=’ColY’, custom_data=[‘Col1’, ‘Col2’, ‘Col3’] ) and then modify it using update_traces and hovertemplate, referencing it as customdata. For example: fig.update_traces( hovertemplate=”<br>”.join([ “ColX: %{x}”, “ColY: %{y}”, “Col1: %{customdata[0]}”, “Col2: %{customdata[1]}”, “Col3: %{customdata[2]}”, ]) ) … Read more

Embedding a Plotly chart in a Django template

Instead of writing the html to a file you can have plotly return the html part of the graph as a string. For example, using a class based TemplateView: EDIT: Update for using recent (as of 2021/08) versions of plotly. The template does not need any changes. import plotly.graph_objects as go class Graph(TemplateView): template_name=”graph.html” def … Read more

How to get Plotly.js default colors list?

According to the source code, the default color list is: ‘#1f77b4’, // muted blue ‘#ff7f0e’, // safety orange ‘#2ca02c’, // cooked asparagus green ‘#d62728’, // brick red ‘#9467bd’, // muted purple ‘#8c564b’, // chestnut brown ‘#e377c2’, // raspberry yogurt pink ‘#7f7f7f’, // middle gray ‘#bcbd22’, // curry yellow-green ‘#17becf’ // blue-teal If you have more … Read more

Use Pandas index in Plotly Express

Reference: https://plot.ly/python/px-arguments/#using-the-index-of-a-dataframe You can pass the index as reference explicitly. So in your case, this would be: import plotly.express as px iris = px.data.iris() fig = px.scatter(iris, x=iris.index, y=”sepal_length”) fig.show() — BONUS QUESTION: what if iris has a pd.MultiIndex? Use pd.MultiIndex.get_level_values. import plotly.express as px # dummy example for multiindex iris = px.data.iris().set_index([‘species’, ‘species_id’, iris.index]) … Read more

Same scale for x and y axis

Finally, this feature is implemented. layout = go.Layout(yaxis=dict(scaleanchor=”x”, scaleratio=1)) Update: in new versions of plotly, use the following: fig.update_yaxes( scaleanchor=”x”, scaleratio=1, ) See example here https://plot.ly/python/axes/#fixed-ratio-axes.

How to hide legend with Plotly Express and Plotly

After creating the figure in plotly, to disable the legend you can make use of this command: fig.update_layout(showlegend=False) For Advanced users: You can also enable/disable the legend for individual traces in a figure by setting the showlegend property of each trace. For instance: fig.add_trace(go.Scatter( x=[1, 2], y=[1, 2], showlegend=False)) You can view the examples here: … Read more

How to set the range of the y axis

Update for newer versions When setting up a figure you can use plotly’s magic underscore notation and specify layout_yaxis_range=[<from_value>, <to_value>] like this: fig = go.Figure(data=go.Scatter(x=x, y=y, mode=”lines”), layout_yaxis_range=[-4,4]) Or if you’ve already got a figure named fig, you can use: fig.update_layout(yaxis_range=[-4,4]) And: fig.update(layout_yaxis_range = [-4,4]) Or: fig.update_yaxes(range = [-4,4]) Figure: Complete code: # imports import … Read more

Plotly chart not showing in Jupyter notebook

You need to change init_notebook_mode call and remove connected=True, if you want to work in offline mode. Such that: # Import the necessaries libraries import plotly.offline as pyo import plotly.graph_objs as go # Set notebook mode to work in offline pyo.init_notebook_mode() # Create traces trace0 = go.Scatter( x=[1, 2, 3, 4], y=[10, 15, 13, 17] … Read more