How to manually set the color of points in plotly express scatter plots

For that you may use the color_discrete_sequence argument. fig = px.scatter(df, x=”sepal_width”, y=”sepal_length”, color_discrete_sequence=[‘red’]) This argument is to use a custom color paletter for discrete color factors, but if you are not using any factor for color it will use the first element for all the points in the plot. More about discrete color palletes: … Read more

How to combine scatter and line plots using Plotly Express

You can use: fig3 = go.Figure(data=fig1.data + fig2.data) Where fig1 and fig2 are built using px.line() and px.scatter(), respectively. And fig3 is, as you can see, built using plotly.graph_objects. Some details: One approach that I use alot is building two figures fig1 and fig2 using plotly.express and then combine them using their data attributes together … Read more

How to define colors in a figure using Plotly Graph Objects and Plotly Express

First, if an explanation of the broader differences between go and px is required, please take a look here and here. And if absolutely no explanations are needed, you’ll find a complete code snippet at the very end of the answer which will reveal many of the powers with colors in plotly.express Part 1: The … Read more

How to save plotly express plot into a html or static image file?

Updated answer: With newer versions of plotly, static Image export in Python is a breeze. Just make sure to install kaleido using: pip install -U kaleido or, for Anaconda: conda install -c conda-forge python-kaleido And then run fig.write_image(“yourfile.png”) Filetypes such as .jpeg and .pdf are also available options. Producing an individual html file is still … Read more

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/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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)