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

How to force Plot.ly Python to use a given yaxis range?

正如您在下面看到的,我手动定义了每个 yaxis 的范围,并将自动范围选项设置为 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)

所以我尝试了您的代码并能够重现该问题。

原因:

原因是,如果您查看左上图的 yaxis,您会看到有 3 个值 [0, 10, 20],因此每个值之间存在 10 的差异价值。因此,当您将范围设置为 [0, 25] 时,不满足 10 的差异,因此我们无法在 yaxis 中看到 25

如果我们查看左下方 x 轴上的图表,我们可以看到值 30 服从每个值之间 10 的差异。这样我们就可以在yaxis中看到30

解法:

如果您查看 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"

所以,当我们将 dtick 设置为 5 并将范围设置为 [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)