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)
Related
Hi I am trying to use plotly to create a 100% stacked bar chart in streamlit using plotly. I tried using relative but to no avail.
dfCategory = dfQuery.groupby(['l1_category_name','pricingPosition'])['pricingPosition'].count().reset_index(name="count")
fig = px.bar(dfCategory, x="count", y="l1_category_name", color='pricingPosition', orientation='h',
height=400)
fig.update_layout(barmode='relative')
st.plotly_chart(fig, use_container_width=True)
Any help would be greatly appreciated
I think the 'relative' mode in Plotly is a function to summarize positive and negative, not a display in percentages. The histogram function has the ability to stack percentages, which can also be displayed as percentages. Here is an example from the official reference.
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
fig.update_layout(barmode='relative')
fig.show()
import plotly.express as px
long_df = px.data.medals_long()
fig = px.histogram(long_df, x="nation",
y="count", color="medal",
barnorm='percent', text_auto='.2f',
title="Long-Form Input")
fig.show()
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)
I am new to plotly and wanted to visualize some data. I got this plot. see here
But I want to get this in 2 or more column based so that it can be seen better.
Can someone help me with that. Here is my source code what I have tried:
import pandas as pd
import plotly.express as px
fig = px.scatter(data2, x = "Total_System_Cost", y= "Total_CO2_Emissions",
color="Pol_Inst", symbol="Pol_Inst",
facet_row='Technologie',width=600, height=3500)
fig.show()
And the data looks like this.here
In this case you should use facet_col and facet_col_wrap as in this example
import pandas as pd
import plotly.express as px
fig = px.scatter(data2,
x="Total_System_Cost",
y="Total_CO2_Emissions",
color="Pol_Inst",
symbol="Pol_Inst",
facet_col='Technologie',
facet_col_wrap=2, #eventually change this
)
fig.show()
If you then want to use width and height do it so according to data2['Technologie'].nunique() and the value you picked for facet_col_wrap.
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()
I have a plotly-dash dashboard and I can't seem to rescale my secondary y-axis. Is there a way of doing this?
I've tried messing with the domain parameter and the range parameter in the go.Layout.
I need the volume bar chart to be scaled down and occupy maybe 10% of the height of the plot so it doesn't overlap with my candlesticks.
Thank you very much.
Any help is appreciated.
import pandas as pd
import pandas_datareader.data as web
import plotly.offline as pyo
import plotly.graph_objs as go
stock_ticker='AAPL'
start_date='2019-04-01'
end_date='2019-05-22'
data=[]
hist_stock_df = web.DataReader(stock_ticker,'iex',start_date, end_date)
data.append(go.Candlestick(x=hist_stock_df.index,
open=hist_stock_df['open'],
high=hist_stock_df['high'],
low=hist_stock_df['low'],
close=hist_stock_df['close'],
name='AAPL'))
data.append(go.Bar(x=hist_stock_df.index,
y=hist_stock_df['volume'].values,
yaxis='y2'))
#y0=1000000
layout=go.Layout(title= 'Candestick Chart of AAPL',
xaxis=dict(title='Date',rangeslider=dict(visible=False)),
yaxis=dict(title='Price'),
plot_bgcolor='#9b9b9b',
paper_bgcolor='#9b9b9b',
font=dict(color='#c4c4c4'),
yaxis2=dict(title='Volume',
overlaying='y',
side='right'))
#scaleanchor='y'))
#scaleratio=0.00000001,
#rangemode='tozero',
#constraintoward='bottom',
#domain=[0,0.1]))
fig = go.Figure(data=data, layout=layout)
pyo.iplot(fig)
I have tried messing with the commented parameters
UPDATE
With this combination of layout parameters I managed to rescale the bars, but now there are two x-axis, been trying to figure out how to bring the middle x-axis down.
layout=go.Layout(title= 'Candestick Chart of AAPL',
xaxis=dict(title='Date',rangeslider=dict(visible=False)),
yaxis=dict(title='Price'),
plot_bgcolor='#9b9b9b',
paper_bgcolor='#9b9b9b',
font=dict(color='#c4c4c4'),
yaxis2=dict(title='Volume',
overlaying='y',
side='right',
scaleanchor='y',
scaleratio=0.0000001))
Use secondary_y=True or secondary_y=False within fig.update_yaxes() to specify which axis to adjust.
Plot 1: Without manual adjustments
Plot 2: With manual adjustments
Code:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np
import datetime
# data
np.random.seed(1234)
numdays=20
dates = pd.date_range('1/1/2020', periods=numdays)
A = (np.random.randint(low=-10, high=10, size=numdays).cumsum()+100).tolist()
B = (np.random.randint(low=0, high=100, size=numdays).tolist())
df = pd.DataFrame({'A': A,'B':B}, index=dates)
# plotly figure setup
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(name='A', x=df.index, y=df['A'].values))
fig.add_trace(go.Bar(name='B', x=df.index, y=df['B'].values), secondary_y=True)
# plotly manual axis adjustments
fig.update_yaxes(range=[50,160], secondary_y=False)
fig.update_yaxes(range=[-10,200], secondary_y=True)
fig.show()