Reputation: 303
I'm using plotly to create some plots. I haven't modified the yaxis range, but I don't like how the chart is displaying the y axis. In the image attached the chart y axis numbers go from 25 to 40, but I have data that is slightly above 40 and below 25. Is there an option to force the y axis to always be higher than the min and max value that I have? Also, is it possible to have the y axis starting at the edges? (Highlighted in purple)
Thank you for your time!
Edit: As asked in the comments, I've added my code below:
fig= go.Figure()
fig.update_layout(
template="simple_white",
font=_a_font,
height=400,
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="left",
x=0,
font=dict(size=16),
itemclick=False,
itemdoubleclick=False,
),
)
fig.update_layout(xaxis=dict(range=x_range, mirror=True, tickmode='linear', tick0=0,dtick=1))
fig.update_yaxes(showgrid=True,mirror=True)
fig.add_trace(go.Scatter(x=sub_x_data,
y=y,
mode="lines+markers",
name=legend_name,
line=dict(color=get_driver_color(legend_name)),
textposition="bottom center",
text = h_data,
hoverinfo="text"))
Upvotes: 1
Views: 3446
Reputation: 506
Here is my approach in Javascript, sure you can adapt it to python, plotly for python is far more configurable than JS. You may set the range of the y axis calculating min and max of your y data, then get the round min and max for the dtick selected (dtick is number of separation of each tick of the scale) for example:
var miny = Math.min.apply(Math, y);
var maxy = Math.max.apply(Math, y);
var dtick = 10;
miny = miny-(miny % dtick);
maxy = maxy + dtick-(maxy % dtick);
var layout= {
yaxis:{
dtick:dtick,
range:[miny,maxy]
}
}
This sets the max and min of the scale above and below the min and max of your dataset, except in the case that any of those values are divisible by the "dtick" number. If your higher value for your y data is 24 and your dtick is 10, then it will set the max of your y axis in 30, if your dtick is 5 it will set the max in 25.
Upvotes: 0
Reputation: 19600
It would be helpful to see your code, as this chart doesn't look like a default style chart in Plotly.
That being said, you should be able to set the range manually, and specify the location of the first tick: fig.update_yaxes(range=[20, 45], tick0=0)
Upvotes: 2