I tried producing a mixed line and bar chart using the same date axis. There are no issues with the code below. But when I include the width argument, doesn’t matter what value I set, the bars will always disappear.
fig = px.line(
df,
x= date_axis,
y= ydata,
color= cdata,
)
for trace in traces:
fig.add_bar(
x= date_axis,
y= ydata2,
opacity = 0.5,
**width = value,**
)
Note that the bar for AAPL is deselected, and it does not show up together with either the bar of MSFT or NFLX.
Hi @divadlen,
There seems to be missing pieces in your code. Why are you looping through traces
?
When posting code on the forum try to create a minimal working example which other members can readily run and help you.
There may be other reasons for the bars not appearing, I was not able to reproduce the error.
Below is a small example of a line and a bar chart with the width argument which works as intended.
import plotly.express as px
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')
fig.add_bar(
x= df['year'],
y= df['lifeExp'],
opacity = 0.5,
name='Yearly',
width = 3
)
fig.show()
2 Likes
The reason why I looped through the traces is because I’m using two different dataframes to produce the plot.
The first one generates lines for all columns for the ydata, for each unique value in color column. Hence you see three different lines for each ticker, but sharing one legend.
Then I add a bar trace for each unique value in color column, hence a new legend for each ticker. However, since they share the same date axis, having fat bars kind of push them away from the reference date, hence my need to change their width.
My current fix is to simply hide those bars in behind legend, and only display them one at a time when clicked. Of course, after disabling the argument for bar width.
Anyway, thanks for the reply. I did not end up getting a solution to my original problem, but I’m satisfied with the current work around.
1 Like