如何强制 Plot.ly Python 使用给定的 yaxis 范围? [英] How to force Plot.ly Python to use a given yaxis range?

查看:20
本文介绍了如何强制 Plot.ly Python 使用给定的 yaxis 范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示,我手动定义了每个 y 轴的范围,并将自动范围选项设置为 False.

As you can see below, I manually define the range for each yaxis as well as setting the autorange option to be False.

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

However, if you graph this, you will still find the yaxis1 range is 0 to 20 rather than 0 to 25. As a result, one of the bars sticks out of the chart.

我该如何做才能确定每个值都包含在 y 轴范围内?

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

Additionally, the top grid line in the second row is not showing. If I rescale slightly, it will appear again. So the issue seems to be purely graphical. Any ideas are appreciated.

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)

推荐答案

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

So I tried your code and was able to replicate the issue.

原因:

造成这种情况的原因是,如果您查看左上图的 y 轴,您会看到有 3 个值 [0, 10, 20],因此 存在差异>10,在每个值之间.所以当你将范围设置为 [0, 25] 时,10 的差异不满足,因此我们看不到 25y轴.

The cause for this, is that, if you look at the top left graph's yaxis you can see there are 3 values [0, 10, 20], so there is a difference of 10, between each of the values. so when you set the range as [0, 25], the difference of 10 is not met, hence we not able to see 25 in the yaxis.

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

If we look at the graph on the bottom left's xaxis, we can see that the value 30 obeys the difference of 10, between each of the values. Thus we are able to see 30 in the yaxis!

解决方案:

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

If you look at the plotly documentation, found here, we can use a particular property of the yaxis object to set the increment between each of the ticks, called dtick, plotly defines it as:

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

P.S: A personal Thank you to Maximilian Peters for aiding to find the solution!!!!

dtick(数字或分类坐标字符串)
设置步长此轴上的中间刻度.与 tick0 一起使用.必须是正数数字,或可用于日志"和日期"轴的特殊字符串.如果轴 type 是log",然后每 10^(n"dtick) 设置一次刻度,其中 n 是刻度数.例如,要在 1、10、100、1000 处设置刻度线,... 将 dtick 设置为 1.将刻度线设置为 1、100、10000,... 设置 dtick到 2. 要在 1、5、25、125、625、3125、... 处设置刻度线,请将 dtick 设置为log_10(5),或 0.69897000433.log"有几个特殊值;"L",其中 f 是一个正数,给出线性间隔的刻度值(但不是位置).例如 tick0 = 0.1, dtick = "L0.5" 将将刻度放在 0.1、0.6、1.1、1.6 等处.显示 10 的幂加小数字之间,使用D1"(所有数字)或D2"(仅 2 和 5).tick0D1"和D2"被忽略.如果轴 type 是日期",那么你必须将时间转换为毫秒.例如,要设置刻度之间的间隔为一天,将 dtick 设置为 86400000.0.日期"也有特殊值 "M" 给出由多个间隔的刻度个月.n 必须是正整数.在 15 日设置刻度每三个月,将 tick0 设置为2000-01-15",将 dtick 设置为M3".到每 4 年设置一次,将 dtick 设置为 "M48"

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] 时,我们将得到预期的结果!

So, when we set the dtick as 5 and the range as [0,25] we will get the expected result!

请试用以下代码,如果您的问题完全解决,请告诉我!

Please tryout the below code and let me know if your issue is resolved completely!

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)

这篇关于如何强制 Plot.ly Python 使用给定的 yaxis 范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆