How to change variable/label names for the legend in a plotly express line chart

The answer: Without changing the data source, a complete replacement of names both in the legend, legendgroup and hovertemplate will require: newnames = {‘col1′:’hello’, ‘col2’: ‘hi’} fig.for_each_trace(lambda t: t.update(name = newnames[t.name], legendgroup = newnames[t.name], hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name]) ) ) Plot: The details: Using fig.for_each_trace(lambda t: t.update(name = newnames[t.name])) …you can change the names in … Read more

Set the range of the y axis in Plotly

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

Saving multiple plots into a single html

In the Plotly API there is a function to_html which returns HTML of the figure. Moreover, you can set option param full_html=False which will give you just DIV containing figure. You can just write multiple figures to one HTML by appending DIVs containing figures: with open(‘p_graph.html’, ‘a’) as f: f.write(fig1.to_html(full_html=False, include_plotlyjs=”cdn”)) f.write(fig2.to_html(full_html=False, include_plotlyjs=”cdn”)) f.write(fig3.to_html(full_html=False, include_plotlyjs=”cdn”)) … Read more

Plotly express vs. Altair/Vega-Lite for interactive plots

Trying to not get into personal preferences and too many details, here are some of the main similarities and differences between the two as far I am aware. Design principles Both Plotly express and Altair are high level declarative libraries, which means you express yourself in terms of data and relationships (like in seaborn, holoviews, … Read more

Plotly/Dash display real time data in smooth animation

Updating traces of a Graph component without generating a new graph object can be achieved via the extendData property. Here is a small example that appends data each second, import dash import dash_html_components as html import dash_core_components as dcc import numpy as np from dash.dependencies import Input, Output # Example data (a circle). resolution = … Read more

How to embed Plotly graphs in Sphinx documentation and nbsphinx

I can see two solutions to embed your notebook cells with plotly figures in a sphinx documentation. Convert the notebook to html using nbconvert or nbsphinx. Be sure to use the notebook renderer in order to include the plotly javascript bundle (maybe this was the reason why your figures did not display): see https://plot.ly/python/renderers/ for … Read more

How to plot multiple lines on the same y-axis using Plotly Express in Python

Short answer: fig = px.line(df, x=’Date’, y=df.columns[1:-6]) Where df.columns are the column names of the columns returned as a list, or a subset of the columns using, for example, df.columns[1:-6] The details Your code works fine But if you specifically do not want to apply the (somewhat laborious) add_trace() function to each line, you can … Read more

how to hide plotly yaxis title (in python)?

Solution You need to use visible=False inside fig.update_yaxes() or fig.update_layout() as follows. For more details see the documentation for plotly.graph_objects.Figure. # Option-1: using fig.update_yaxes() fig.update_yaxes(visible=False, showticklabels=False) # Option-2: using fig.update_layout() fig.update_layout(yaxis={‘visible’: False, ‘showticklabels’: False}) # Option-3: using fig.update_layout() + dict-flattening shorthand fig.update_layout(yaxis_visible=False, yaxis_showticklabels=False) Try doing the following to test this: # Set the visibility ON … Read more