gpt4 book ai didi

python - plotly python : force axis limits (range)

转载 作者:行者123 更新时间:2023-12-02 04:03:55 25 4
gpt4 key购买 nike

Documentation of plotly说,使用参数 range 我们可以设置轴的限制,例如 range = [0, 10] 将轴最小值设置为 0,最大值设置为 10。根据docs,它可以在图形、布局、场景或xaxis下使用。事实上,ScatterLayout 接受 xaxisyaxis 参数,它们是字典或 plotly.graph_objs。 XAxis 对象,并且可以提供一个 range 值。然而,尝试了我能想象到的所有变化,它显然无法设定限制。此外,第一个子图似乎低于其他图。请参阅下面的最小工作示例,它可以在任何 Jupyter 笔记本中运行。

奖励问题:为什么 Scatter 中的 fill 参数无法设置点的填充颜色?

import plotly
import plotly.tools
import plotly.graph_objs
plotly.offline.init_notebook_mode()

data = {
'A': {
'x': [1.0, 2.0, 6.0, 8.0],
'y': [34.0, 36.0, 38.0, 40.0],
's': [0.00416, 0.01125, 0.0038, 0.002]
},
'B': {
'x': [1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 3.0, 2.0, 2.0, 3.0,
4.0, 2.0, 4.0, 5.0, 6.0, 7.0, 7.0],
'y': [30.0, 32.0, 32.0, 33.0, 34.0, 34.0, 34.0, 35.0,
36.0, 36.0, 36.0, 38.0, 38.0, 38.0, 38.0, 38.0, 40.0],
's': [0.029999999999999999, 0.19625000000000001, 0.070833333333333331,
0.0079166666666666673, 0.23749999999999999, 0.37708333333333333,
0.028333333333333332, 0.018749999999999999, 0.51875000000000004,
0.066666666666666666, 0.02375, 0.0066666666666666671,
0.012083333333333333, 0.01125, 0.016666666666666666,
0.0058333333333333336, 0.0275]
},
'C': {
'x': [1.0, 2.0, 1.0, 2.0, 2.0],
'y': [32.0, 32.0, 34.0, 34.0, 36.0],
's': [0.029208333333333333, 0.0050000000000000001, 0.03820833333333333,
0.022833333333333334, 0.029083333333333333],
},
'D': {
'x': [0.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.0, 2.0, 3.0, 1.0, 1.0,
2.0, 3.0, 4.0, 5.0, 6.0, 3.0, 4.0, 5.0, 6.0, 7.0, 6.0, 7.0],
'y': [30.0, 30.0, 31.0, 31.0, 32.0, 32.0, 33.0, 33.0, 34.0, 34.0, 34.0,
35.0, 36.0, 36.0, 36.0, 36.0, 36.0, 36.0, 38.0, 38.0, 38.0, 38.0,
38.0, 40.0, 40.0],
's': [0.0087500000000000008, 0.050000000000000003, 0.008750000000000000,
0.013333333333333334, 0.60875000000000001, 0.16666666666666666,
0.04583333333333333, 0.00070833333333333338, 0.73320833333333335,
0.54541666666666666, 0.040833333333333333, 0.02, 0.0700000000000,
0.73124999999999996, 0.1125, 0.066666666666666666, 0.02083333332,
0.0083333333333333332, 0.027916666666666666, 0.0212500000000000,
0.070833333333333331, 0.11666666666666667, 0.040833333333333333,
0.059999999999999998, 0.1125]
}
}

xlim = [0, 9]
ylim = [13, 60]

traces = []

for name in sorted(data.keys()):
sub = data[name]

traces.append(plotly.graph_objs.Scatter(x = sub['x'], y = sub['y'],
mode = 'markers',
marker = dict(
size = sub['s'],
sizemode = 'area',
sizeref = 0.0001),
name = name,
fill = '#333333',
showlegend = False,
xaxis = dict(range = xlim),
yaxis = dict(range = ylim))
)

fig = plotly.tools.make_subplots(rows=2,
cols=2,
subplot_titles=sorted(data.keys())
)

for i, trace in enumerate(traces):
fig.append_trace(trace, row = i // 2 + 1, col = (i % 2) + 1)

fig['layout'].update(height = 1000, width = 600, title = main_title,
xaxis = dict(range = xlim), yaxis = dict(range = ylim))

plotly.offline.iplot(fig, show_link = False)

结果是这样的:

failed to set axes limits

最佳答案

看起来它在 plotly=5.1.0 中按预期工作。

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=2, subplot_titles=sorted(data.keys()))
for i, name in enumerate(sorted(data.keys())):
sub = data[name]
fig.add_trace(
go.Scatter(
x = sub['x'],
y = sub['y'],
mode = 'markers',
marker = dict(
size = sub['s'],
sizemode = 'area',
sizeref = 0.0001,
),
name = name,
showlegend = False,
),
row = i // 2 + 1,
col = (i % 2) + 1,
)
fig.update_xaxes(range=[0, 9])
fig.update_yaxes(range=[13, 60])
fig

scatter grid

关于python - plotly python : force axis limits (range),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470748/

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