add a trace to every facet of a plotly figure - python

I'd like to add a trace to all facets of a plotly plot.
For example, I'd like to add a reference line to each daily facet of a scatterplot of the "tips" dataset showing a 15% tip. However, my attempt below only adds the line to the first facet.
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
df = px.data.tips()
ref_line_slope = 0.15 # 15% tip for reference
ref_line_x_range = np.array([df.total_bill.min(), df.total_bill.max()])
fig = px.scatter(df, x="total_bill", y="tip",facet_col="day", trendline='ols')
fig = fig.add_trace(go.Scatter(x=reference_line_x_range,y=ref_line_slope*reference_line_x_range,name='15%'))
fig.show()

According to an example from plotly you can pass 'all' as the row and col arguments and even skip empty subplots:
fig.add_trace(go.Scatter(...), row='all', col='all', exclude_empty_subplots=True)

It's not an elegant solution, but it should work for most cases
for row_idx, row_figs in enumerate(fig._grid_ref):
for col_idx, col_fig in enumerate(row_figs):
fig.add_trace(go.Scatter(...), row=row_idx+1, col=col_idx+1)

Related

Outliers not showing up in plotly

Hi I am using this code to generate a boxplot:
However, it is not showing all the data points also not showing up the outliers on the top. I am using this code:
import plotly.express as px
fig = px.box(dataset, y= 'Report_used',hover_name='entity_name')
fig.show()
It needs to be like this:
How can I do this?
Thanks
import plotly.express as px
fig = px.box(dataset,
y= 'Report_used',
hover_name='entity_name',
points='all')
fig.update_traces(pointpos=0)
fig.show()
If you wanted all the points to be on the whisker line:
fig.update_traces(pointpos=0, jitter=0)

How to add a secondary Y axis to a Plotly Express bar plot?

I would like to add a second Y axis to my bar plot bellow, that is the number of citizens in integer:
this graph was made using plotly:
import plotly.express as px
fig = px.bar(df, x="country",y="pourcent_visit",color="city",barmode='group')
# fig.add_hline(y=10)
fig.show()
To my knowledge, there's no direct way to do this. But you can easily build a Plotly Express figure, grab the traces (and data structures) from there and combine them in a figure that allows multiple axes using fig = make_subplots(specs=[[{"secondary_y": True}]]). With no provided data sample, I'll use the built-in dataset px.data.tips() that I'm guessing to a large part resembles the structure of your real world dataset judging by the way you've applied the arguments in px.bar(). Details in the comments, but please don't hesitate to let me know if something is unclear.
Plot:
Complete code:
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# sample data
df = px.data.tips()
# figure setup with multiple axes
fig = make_subplots(specs=[[{"secondary_y": True}]])
# build plotly express plot
fig2 = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group")
# add traces from plotly express figure to first figure
for t in fig2.select_traces():
fig.add_trace(t, secondary_y = False)
# handle data for secondary axis
df2 = df.groupby('day').agg('sum')#.reset_index()
df2 = df2.reindex(index = df['day'].unique()).reset_index()
#
fig.add_trace(go.Scatter(x = df2['day'], y = df2['size'], mode = 'lines'), secondary_y = True)
# fix layout
fig.update_layout(legend_title_text = 'smoker')
fig.show()

How to update range_color in Plotly Express?

I am using plotly express and I want to display some data:
import plotly.express as px
dfx = px.data.tips()
fig = px.scatter(dfx,
x='total_bill', y='tip',
color='size',
template='plotly_dark',
range_color=[2,4])
My goal is to update the range color after it has been defined.
I tried something like this:
fig.update_layout(range_color=[3,6])
ValueError: Invalid property specified for object of type plotly.graph_objs.Layout: 'range'
but without success.
Are you aware of what I need to write in order to update the range color values?
To change the range of the color bar, you would change the maximum and minimum values of the color axis. This is different from the description of the graph settings, which can be found in fig.layout.
import plotly.express as px
dfx = px.data.tips()
fig = px.scatter(dfx,
x='total_bill', y='tip',
color='size',
template='plotly_dark',
range_color=[2,4])
fig.update_layout(coloraxis=dict(cmax=6, cmin=3))
fig.show()
I think you need to use range_color in the px.scatter function
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df,
x="sepal_width",
y="sepal_length",
color="sepal_length",
color_continuous_scale=["red",
"green", "blue"])
fig.show()
Here is a link to the documentation plotly
You can also look here at fig.update_coloraxes
update coloraxes
Also just found this in the documentation for update_layout
fig.update_layout(colorscale=dict(...))
update colorscale

How to disable trendline in plotly.express.line?

I am willing to plot 3 timeseries on the same chart. Datasource is a pandas.DataFrame() object, the type of Timestamp being datetime.date, and the 3 different time series drawn from the same column Value using the color argument of plotly.express.line().
The 3 lines show on the chart, but each one is accompanied by some sort of trendline. I can't see in the function signature how to disable those trendlines. Can you please help?
I have made several attempts, e.g. using another color, but the trendlines just stay there.
Please find below the code snippet and the resulting chart.
import plotly.io as pio
import plotly.express as px
pio.renderers = 'jupyterlab'
fig = px.line(data_frame=df, x='Timestamp', y='Value', color='Position_Type')
fig.show()
(If relevant, I am using jupyterlab)
Timestamp on the screen appears like this (this are [regular] weekly timeseries) :
And, as per the type:
type(df.Timestamp[0])
> datetime.date
I am adding that it looks like the lines that I first thought were trendlines would rather be straight lines from the first datapoint to the last datapoint of each time series.
df_melt = df_melt.sort_values('datetime_id')
Sorting got rid of those "wrap-arounds". Thanks for the suggestions above. Using Plotly 4.8.2.
Introduction:
Your provided data sample is an image, and not very easy to work with, so I'm going to use some sampled random time series to offer a suggestion. The variables in your datasample don't match the ones you've used in px.Scatter either by the way.
I'm on plotly version '4.2.0' and unable to reproduce your issue. Hopefully you'll find this suggestion useful anyway.
Using data structured like this...
Timestamp Position_type value
145 2020-02-15 value3 86.418593
146 2020-02-16 value3 78.285128
147 2020-02-17 value3 79.665202
148 2020-02-18 value3 84.502445
149 2020-02-19 value3 91.287312
...I'm able to produce this plot...
...using this code:
# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np
# data
np.random.seed(123)
frame_rows = 50
n_plots = 2
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
index=pd.date_range('1/1/2020', periods=frame_rows),
columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100
df.reset_index(inplace=True)
df.columns=['Timestamp','value1', 'value2', 'value3' ]
varNames=df.columns[1:]
# melt dataframe with timeseries from wide to long format.
# YOUR dataset seems to be organized in a long format since
# you're able to set color using a variable name
df_long = pd.melt(df, id_vars=['Timestamp'], value_vars=varNames, var_name='Position_type', value_name='value')
#df_long.tail()
# plotly time
import plotly.io as pio
import plotly.express as px
#pio.renderers = 'jupyterlab'
fig = px.scatter(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
#fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
fig.show()
If you change...
px.scatter(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
...to...
fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
...you'll get this plot instead:
No trendlines as far as the eye can see.
Edit - I think I know what's going on...
Having taken a closer look at your figure, I've realized that those lines are not trendlines. A trendline doesn't normally start at the initial value of a series and end up at the last value of the series. And that's what happening here for all three series. So I think you've got some bad or duplicate timestamps somewhere.

Plotly: How to make line charts colored by a variable using plotly.graph_objects?

I'm making a line chart below. I want to make the lines colored by a variable Continent. I know it can be done easily using plotly.express
Does anyone know how I can do that with plotly.graph_objects? I tried to add color=gapminder['Continent'], but it did not work.
Thanks a lot for help in advance.
import plotly.express as px
gapminder = px.data.gapminder()
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=gapminder['year'], y=gapminder['lifeExp'],
mode='lines+markers'))
fig.show()
Using an approach like color=gapminder['Continent'] normally applies to scatterplots where you define categories to existing points using a third variable. You're trying to make a line plot here. This means that not only will you have a color per continent, but also a line per continent. If that is in fact what you're aiming to do, here's one approach:
Plot:
Code:
import plotly.graph_objects as go
import plotly.express as px
# get data
df_gapminder = px.data.gapminder()
# manage data
df_gapminder_continent = df_gapminder.groupby(['continent', 'year']).mean().reset_index()
df = df_gapminder_continent.pivot(index='year', columns='continent', values = 'lifeExp')
df.tail()
# plotly setup and traces
fig = go.Figure()
for col in df.columns:
fig.add_trace(go.Scatter(x=df.index, y=df[col].values,
name = col,
mode = 'lines'))
# format and show figure
fig.update_layout(height=800, width=1000)
fig.show()

Categories

Resources