import numpy as np
# Sample data
X = np.arange(10)
Y1 = np.sin(X)
Y2 = np.cos(X)
Z = np.array([2, 1, 4, 3, 5, 3, 1, 2, 4, 5])
# Create a Plotly figure
fig = go.Figure()
# Add line traces
fig.add_trace(go.Scatter(x=X, y=Y1, mode='lines', name='Sin(X)', line=dict(color='blue')))
fig.add_trace(go.Scatter(x=X, y=Y2, mode='lines', name='Cos(X)', line=dict(color='green')))
# Add bar trace
fig.add_trace(go.Bar(x=X, y=Z, name='Z value', marker_color='red', opacity=0.4, width=0.3))
# Update layout
fig.update_layout(title='Clustered Bar and Line Chart (Plotly)',
xaxis_title='X-axis',
yaxis=dict(title='Y-axis (Line)', titlefont=dict(color='black')),
yaxis2=dict(title_text='Z-axis (Bar)', titlefont=dict(color='black'), overlaying='y', side='right'))
# Show the plot
fig.show()
However this plot has one defect: It does not display the yaxis2 title ‘Z axis(Bar)’ on the right.
How can I make the title appear?
And also I would like for the two axes to have different values. So the scatter that only goes to 1 will have values till 1 but the bars that go to 5 should be much smaller.
Thanks! This is the solution I was looking for . (In the meantime I also found one using fig.add_trace(go.Bar(x=X, y=Z, yaxis='y2', name='Z value', marker_color='red', opacity=0.4, width=0.3))
Is there any way to:
Display the grid only of the left axis but not the right one?
Modify the range of the right one so that to make the maximum value (5) appear much smaller (something like setting the max to 10)