gpt4 book ai didi

python-3.x - 如何强制 Plot.ly Python 使用给定的 yaxis 范围?

转载 作者:行者123 更新时间:2023-12-03 16:24:44 24 4
gpt4 key购买 nike

如下所示,我手动定义了每个 yaxis 的范围,并将 autorange 选项设置为 False。

但是,如果您绘制此图,您仍会发现 yaxis1 的范围是 0 到 20,而不是 0 到 25。因此,其中一个条形伸出图表。

我如何才能确定每个值都包含在 yaxis 范围内?

编辑:此外,第二行的顶部网格线未显示。如果我稍微重新缩放,它会再次出现。所以这个问题似乎纯粹是图形化的。任何想法表示赞赏。

from plotly import tools
fig = tools.make_subplots(rows=2, cols=2, subplot_titles=['A', 'B'], shared_xaxes=False, shared_yaxes=True)

data = [[10, 4, 15, 20.5], [3, 12, 22.2], [6.5, 12, 26.2], [18, 4.2, 22.2]]
traces = [go.Bar(x=['Type A', 'Type B', 'Type C'], y=d) for d in data]

fig.append_trace(traces[0], 1, 1)
fig.append_trace(traces[1], 1, 2)
fig.append_trace(traces[2], 2, 1)
fig.append_trace(traces[3], 2, 2)

fig['layout']['yaxis1'].update(title='', range=[0, 25], autorange=False)
fig['layout']['yaxis2'].update(title='', range=[0, 30], autorange=False)

py.iplot(fig)

Produced Chart with incorrect axis

最佳答案

所以我尝试了你的代码并且能够复制这个问题。

原因:

造成这种情况的原因是,如果您查看左上图的 yaxis,您可以看到有 3 个值 [0, 10, 20] ,所以有10的区别, 在每个值之间。因此,当您将范围设置为 [0, 25] 时,10的区别未满足,因此我们无法看到 25在yaxis。

如果我们查看左下角 x 轴上的图表,我们可以看到值 30服从10的差异, 在每个值之间。因此我们可以看到 30在yaxis!

解决方案:

如果您查看 plotly 文档,会发现 here ,我们可以使用 yaxis 的特定属性对象设置每个刻度之间的增量,称为 dtick , plotly 地将其定义为:

P.S:个人感谢 Maximilian Peters帮助找到解决方案!!!!

dtick (number or categorical coordinate string)
Sets the step in-between ticks on this axis. Use with tick0. Must be a positive number, or special strings available to "log" and "date" axes. If the axis type is "log", then ticks are set every 10^(n"dtick) where n is the tick number. For example, to set a tick mark at 1, 10, 100, 1000, ... set dtick to 1. To set tick marks at 1, 100, 10000, ... set dtick to 2. To set tick marks at 1, 5, 25, 125, 625, 3125, ... set dtick to log_10(5), or 0.69897000433. "log" has several special values; "L", where f is a positive number, gives ticks linearly spaced in value (but not position). For example tick0 = 0.1, dtick = "L0.5" will put ticks at 0.1, 0.6, 1.1, 1.6 etc. To show powers of 10 plus small digits between, use "D1" (all digits) or "D2" (only 2 and 5). tick0 is ignored for "D1" and "D2". If the axis type is "date", then you must convert the time to milliseconds. For example, to set the interval between ticks to one day, set dtick to 86400000.0. "date" also has special values "M" gives ticks spaced by a number of months. n must be a positive integer. To set ticks on the 15th of every third month, set tick0 to "2000-01-15" and dtick to "M3". To set ticks every 4 years, set dtick to "M48"



所以,当我们设置 dtick5范围为 [0,25]我们会得到预期的结果!

请尝试以下代码,如果您的问题已完全解决,请告诉我!
import pandas as pd
import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()
from plotly import tools
fig = tools.make_subplots(rows=2, cols=2, subplot_titles=['A', 'B'], shared_xaxes=False, shared_yaxes=True)

data = [[10, 4, 15, 20.5], [3, 12, 22.2], [6.5, 12, 26.2], [18, 4.2, 22.2]]
traces = [go.Bar(x=['Type A', 'Type B', 'Type C'], y=d) for d in data]

fig.append_trace(traces[0], 1, 1)
fig.append_trace(traces[1], 1, 2)
fig.append_trace(traces[2], 2, 1)
fig.append_trace(traces[3], 2, 2)

fig['layout']['yaxis1'].update(title='', range=[0, 25], dtick=5, autorange=False)
fig['layout']['yaxis2'].update(title='', range=[0, 30], autorange=False)

py_offline.iplot(fig)

关于python-3.x - 如何强制 Plot.ly Python 使用给定的 yaxis 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50491445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com