简体   繁体   English

如何在 Plotly Python 中设置多索引 x 轴的范围?

[英]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()

链接到示例 2

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:但没有任何变化:

链接到示例 2

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_minx_max结果分别等于-0.32131661442006275.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])

图2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绘制熊猫多索引DataFrame,其中一个索引作为Y轴,另一个作为X轴 - Plotting pandas multi-index DataFrame with one index as Y-axis and other as X-axis 使用 Matplotlib 绘制多索引数据框时 x 轴标签出错 - Error with x-axis labels when plotting multi-index dataframe using Matplotlib 如何在带有子图的 plotly 图中设置辅助 x 轴及其范围? - How to set secondary x-axis and its range in plotly graph with subfigures? 你如何获得 Plotly 图的 x 轴的范围? - How do you get the range of the x-axis of a Plotly graph? 如何在Python API中使用plotly在x轴范围中间位置绘制垂直线? - How to plot a vertical line at the x-axis range median position using plotly in Python API? 如何在Python Plotly中创建具有共享x轴和范围滑块的子图 - How to create a subplot with shared x-axis and range slider in Python Plotly Python Plotly时区错误地设置在x轴烛台中 - Python Plotly timezone incorrectly set in x-axis candlestick 如何在 Python Plotly 中显示时间戳 x 轴 - How to show timestamp x-axis in Python Plotly python - 如何在python plotly express中对x轴标签和图例进行排序? - How to sort x-axis label and legends in python plotly express? 如何设置海洋点图的x轴范围? - How to set the range of x-axis for a seaborn pointplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM