Here's a code:
import plotly
import plotly.express as px
x = data
fig = px.line(x=fruits, y=[1,3,2], color=px.Constant("Line"),
labels=dict(x="Fruit", y="Amount", color="Line"))
fig.add_bar(x=fruits, y=[1,3,1], name="Red")
fig.add_bar(x=fruits, y=[2,1,3], name="Green").update_yaxes(autorange="reversed")
fig.show()
I want to change it, so one bar plot would've been placed normally, and the other would be drawn right opposite the first one, hanging upside down. How can I do it?
Thank you!
You can create a secondary y-axis and reverse the range of values, mapping the first group of bars to the default y-axis, and the second group of bars to the secondary y-axis. Then to make sure the bars don't overlap, you can insert empty data for each group of bars. Also you didn't specify your fruits variable, so I created an arbitrary list for you. Feel free to modify my code to suit your purposes!
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# x = data
fruits = ["apple","pear","orange"]
fig = make_subplots(specs=[[{"secondary_y": True}]])
# fig = px.line(x=fruits, y=[1,3,2], color=px.Constant("Line"),
# labels=dict(x="Fruit", y="Amount", color="Line"))
fig.add_trace(
go.Bar(
x=fruits,
y=[1,3,1],
marker_color="Tomato",
name="Red"
)
)
# empty plot
fig.add_trace(
go.Bar(
x=fruits,
y=[0,0,0],
name=None,
showlegend=False
)
)
# empty values
fig.add_trace(
go.Bar(
x=fruits,
y=[0,0,0],
name=None,
showlegend=False
),
secondary_y=True
)
fig.add_trace(
go.Bar(
x=fruits,
y=[2,1,3],
marker_color="LightGreen",
name="Green",
),
secondary_y=True
)
fig['layout']['yaxis2']['autorange'] = "reversed"
fig.show()
Related
I'm trying to add a point to the last observation on a time series chart with plotly. It is not very different from the example here https://stackoverflow.com/a/72539011/3021252 for instance. Except it is the last observation. Unfortunately following such pattern modifies the axis range.
Here is an example of an original chart
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()
But after adding a marker
import plotly.graph_objects as go
fig.add_trace(
go.Scatter(
x=[df["year"].values[-1]],
y=[df["lifeExp"].values[-1]],
mode='markers'
)
)
It looks like that
Has anyone have an idea how not to introduce this gap on the right?
I'm trying to create a bar graph using two plots but the Y axis doesn't fit with the y values I have assigned to it (Percent Change). I don't see what I have done wrong when creating the bar graph because when I created a scatter graph with the same approach and assigned values it seemed to be working fine. The y axis should be showing 'percent change' that is 10 or higher. While it does so when a scatter graph is created, it doesn't show these values when creating the bar graph. Instead the bar graph shows the random percent change between 0 and 100 which is not in the assigned values table. Is there any way that I can fix this?
I've copied the code below.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
trace1 = go.Bar(
x=df["Date"],
y=TopTesla["Percent Change"],
name='With Tesla',
text=TopTesla['text'],
marker=dict(
color='rgb(30,160,190)'
)
)
trace2 = go.Bar(
x=df["Date"],
y=TopNotTesla["Percent Change"],
name='Without Tesla',
text=TopNotTesla["text"],
marker=dict(
color='rgb(255,200,35)'
),
yaxis='y2',
offset=100,
showlegend=True
)
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace1)
fig.add_trace(trace2,secondary_y=True)
fig['layout'].update(height = 1100, width = 1500,xaxis=dict(
tickangle=-90
))
plt.figure(figsize=[20,25])
iplot(fig)
I made a bar chart with python plotly, and I want to put a marker on a particular bar, example non-smoking females.
Does anyone know how to specify this?
I took an example from the plotly documentation, if I try to put the marker it just takes the center of the main category.
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="sex", y="total_bill",
color='smoker', barmode='group',
height=400)
#trying to set the marker
fig.add_trace(
go.Scatter(x=["Female"],
y=[1100]
))
fig.show()
inspired by this: https://community.plotly.com/t/grouped-bar-charts-with-corresponding-line-chart/19562/4
use xaxis2, work out position, have hardcoded it, but 0.15 has relationship to number of traces in bargoup and x value
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
df = px.data.tips()
fig = px.histogram(
df, x="sex", y="total_bill", color="smoker", barmode="group", height=400
)
# trying to set the marker
fig.add_trace(
go.Scatter(
x=[0.15],
y=[1100],
customdata=[["No", "Female"]],
xaxis="x2",
hovertemplate="smoker=%{customdata[0]}<br>sex=%{customdata[1]}<br>sum of total_bill=%{y}<extra></extra>",
)
)
fig.update_layout(xaxis2={"overlaying": "x", "range": [0, 1], "showticklabels": False})
fig
I have a plotly offline chart with datetime and single y axis,now I want to add one more line in y axis.
original code:
from plotly.offline import download_plotlyjs,init_notebook_mode,plot
plot([Scatter(x=datetimefield,y=value1)],filename="plotly.html")
To add multiple I am tried to tweak the y parameter :
plot([Scatter(x=datecolumn,y=[value1,value2])],filename="plotly.html")
But this doesn't seems to be working.
x=datetime field is time series based
y=value1 & value 2 are two pandas columns
Note:- Two axis are in different datatype one is numeric other is percentage
How to tweak the y parameter in offline mode of plotly to have multiple axis.
Found solution:
from plotly.offline import download_plotlyjs,init_notebook_mode,plot
import plotly.graph_objs as go
trace1 = go.Scatter(
x=df.datetimecolumn,
y=df.value1)
trace2 = go.Scatter(
x=df.datetimecolumn,
y=df.value2)
data = [trace1, trace2]
layout = go.Layout(
xaxis=dict(
zeroline=True,
showline=True,
mirror='ticks',
gridcolor='#bdbdbd',
gridwidth=2,
zerolinecolor='#969696',
zerolinewidth=4,
linecolor='#636363',
linewidth=6
),
yaxis=dict(
zeroline=True,
showline=True,
mirror='ticks',
gridcolor='#bdbdbd',
gridwidth=2,
zerolinecolor='#969696',
zerolinewidth=4,
linecolor='#636363',
linewidth=6
)
)
fig = go.Figure(data=data, layout=layout)
plot(fig)
I have a dataset like this:
I need to plot this dataset based on two criteria: Churn and Cust_Value.
I can do it using seaborn:
sns.barplot(x="Cust_Value", y="value", hue="Churn", data=merged)
plt.show()
The result gives me:
How can I add all variables (value, age, reloads, calls, duration, sms, gprs, inactive) to the same graph as groups of bars?
for that kind of output i would recommend using plotly,
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Bar(
x=DF['Cust_Value'],
y=DF['value'],
name='value'
)
trace2 = go.Bar(
x=DF['Cust_Value'],
y=DF['age'],
name='age'
)
trace3 = go.Bar(
x=DF['Cust_Value'],
y=DF['calls'],
name='calls'
)
data = [trace1, trace2, trace3]
layout = go.Layout(
barmode='group'
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')
sample output:
reference: https://plot.ly/python/bar-charts/
hope it helps!
if it does upvote :)
peace