[英]How to set the range of multi-index x-axis in Plotly Python?
I want to change the range of an x-axis in Plotly Python plot, in which the x-axis has to two indices.我想在 Plotly Python 图中更改 x 轴的范围,其中 x 轴必须有两个索引。 I want to restrict the range from [10,2] to [12,1], but
fig.update_xaxes(range=[[10,2],[12,1]])
seems not to work.我想限制从 [10,2] 到 [12,1] 的范围,但
fig.update_xaxes(range=[[10,2],[12,1]])
似乎不起作用。
That's what I tried:那就是我试过的:
import plotly.graph_objs as go
import pandas as pd
# create data
df = pd.DataFrame({'x1': [10,10,11,11,12,12], 'x2': [1,2,1,2,1,2], 'y': [1, 2, 3, 4, 5, 6]})
# create a scatter plot
fig = go.Figure()
fig.add_trace(go.Scatter(x=[df['x1'], df['x2']], y=df['y']))
fig.show()
Now, I manipulate the x-xaxis with fig.update_xaxes(range=[[10,2],[12,1]])
,现在,我用
fig.update_xaxes(range=[[10,2],[12,1]])
操纵 x-xaxis,
import plotly.graph_objs as go
import pandas as pd
df = pd.DataFrame({'x1': [10,10,11,11,12,12], 'x2': [1,2,1,2,1,2], 'y': [1, 2, 3, 4, 5, 6]})
fig = go.Figure()
fig.add_trace(go.Scatter(x=[df['x1'], df['x2']], y=df['y']))
# set the range of the double-index x-axis
fig.update_xaxes(range=[[10,2],[12,1]])
fig.show()
yet nothing changes:但没有任何变化:
Anybody has any ideas what I am doing wrong?有人知道我做错了什么吗? Or does Plotly not allow what I want to do?
还是 Plotly 不允许我做我想做的事? Thanks so much!
非常感谢!
I am not sure if plotly explicitly allows you to do what you want – I believe the multiindex is interpreted by plotly as numerical x-coordinates, and that the multiindex is displayed over it so plotly doesn't really "understand" the multiindex numbering system you have.我不确定 plotly 是否明确允许你做你想做的事——我相信 multiindex 被 plotly 解释为数字 x 坐标,并且 multiindex 显示在它上面,所以 plotly 并不真正“理解”multiindex 编号系统你有。
One possibility I can suggest is an ugly hack – you first need to pip install -U kaleido
and then can retrieve the default xaxis range that plotly is using with:我可以建议的一种可能性是一个丑陋的 hack——你首先需要
pip install -U kaleido
然后可以检索 plotly 使用的默认 xaxis 范围:
x_min, x_max = fig.full_figure_for_development().layout.xaxis.range
x_min
and x_max
turn out to be equal to -0.3213166144200627
and 5.321316614420063
, respectively. x_min
和x_max
结果分别等于-0.3213166144200627
和5.321316614420063
。 Then you can find the spacing between each tick with:然后你可以找到每个刻度之间的间距:
tick_spacing = (x_max - x_min) / 6
I've hardcoded division by 6, but you can make this solution less brittle by dividing by the number of unique multi-indices you have in your data.我已将除以 6 硬编码,但您可以通过除以数据中的唯一多索引数来使此解决方案不那么脆弱。
And then add or subtract multiples of this tick_spacing
to x_min and x_max.然后将这个
tick_spacing
的倍数加减到 x_min 和 x_max。 For example:例如:
fig.update_xaxes(range=[x_min + tick_spacing, x_max - tick_spacing])
Although it's a multi-categorical axis, the range works as if there's a simple numerical axis.虽然它是一个多分类轴,但范围就像有一个简单的数字轴一样工作。 There are six values, starting at x=0 going to x=5.
有六个值,从 x=0 开始到 x=5。
To show all values显示所有值
fig.update_xaxes(range=[0,5])
To use the range in your example在您的示例中使用范围
fig.update_xaxes(range=[1,4])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.