gpt4 book ai didi

python - 如何使用Python在plotly中标记子图a、b、c、d?

转载 作者:行者123 更新时间:2023-12-01 03:19:33 25 4
gpt4 key购买 nike

我想标记子图,类似于: enter image description here

无论图的比例如何,标签 (a)、(b) 等都位于图上的相同位置。换句话说,这似乎是指定绘图上以英寸或厘米为单位的位置的好方法。我如何在Python中使用plotly来做到这一点?我只见过用 plotting scatter points 进行注释的方法,这似乎在子图中实现完全不同的尺度会很痛苦。

最佳答案

您可以通过layout:annotations来注释您的子图。将 xrefyref 设置为 paper允许您指定相对于总绘图大小的 xy 坐标(箭头也需要通过 showarrow=False 隐藏),例如

fig = plotly.tools.make_subplots(rows=3, cols=2)
fig.append_trace(trace1, 1, 1)
fig['layout'].update(list(annotations=dict(
text='(a)',
xref='paper',
x=0.1,
yref='paper',
y=0.8,
showarrow=False
)))

该图是使用以下代码创建的。它肯定需要更多的调整,但你就会明白了。

enter image description here还需要一些小事情来获得 plotly :

  • 通过 subplot_titles=(('',) * 6) 隐藏子图名称
  • 通过 name='', showlegend=False 隐藏散点图迹线名称
  • 通过 colorbar=dict(showticklabels=False) 隐藏直方图的刻度标签
  • 通过 showscale=False 隐藏一个直方图颜色条

祝你好运!

import plotly
import numpy

n = 50000 #number of random points for heatmaps/histograms

#location of the annotations
anno_x = [0.05, 0.6]
anno_y = [0.8, 0.00]

#the ranges of all subplots, [row][x, y]
row_range = list()
row_range.append([[0, 7.25], [35, 105]])
row_range.append([[25, 225], [425, 775]])
row_range.append([[0, 5.25], [-5.25, 2.75]])

#label annotations for each subplot
labels = ['(a)', '(b)', '', '', '(c)', '(d)']

#create the subplot
fig = plotly.tools.make_subplots(rows=3, cols=2, subplot_titles=(('',) * 6))

#assign the annotations
annotations = list()
for i, label in enumerate(labels):
if label:
annotations.append(dict(text=label, x=anno_x[i % 2], y=anno_y[i // 3], xref='paper', yref='paper', showarrow=False))

else:
annotations.append(dict(showarrow=False, text=''))

fig['layout'].update(annotations=annotations)

#let's create some random data
trace1 = plotly.graph_objs.Scatter(
x=[i for i in range(8)],
y=[100 - i * numpy.random.randint(7) for i in range(8)],
name='',
showlegend=False,
)
fig.append_trace(trace1, 1, 1)

trace2 = plotly.graph_objs.Scatter(
x=[i for i in range(8)],
y=[100 - i * 5 + numpy.random.randint(2) for i in range(8)],
name='',
showlegend=False,
)
fig.append_trace(trace2, 1, 2)

#some random points for the 1st heatmap/histogram
trace3 = plotly.graph_objs.Histogram2d(
x=numpy.random.normal(loc=80, scale=50, size=n),
y=numpy.random.normal(loc=525, scale=200, size=n),
autobinx=False,
autobiny=False,
xbins=dict(start=row_range[1][0][0], end=row_range[1][0][1], size=5),
ybins=dict(start=row_range[1][1][0], end=row_range[1][1][1], size=5),
colorbar=dict(showticklabels=False)
)
fig.append_trace(trace3, 2, 1)

trace4 = plotly.graph_objs.Histogram2d(
x=numpy.random.normal(loc=50, scale=100, size=n),
y=numpy.random.normal(loc=550, scale=200, size=n),
showscale=False,
colorbar=dict(showticklabels=False)
)
fig.append_trace(trace4, 2, 2)

trace5 = plotly.graph_objs.Scatter(
x=[i for i in range(6)],
y=[0 - i + numpy.random.rand() for i in range(6)],
name='',
showlegend=False,
)
fig.append_trace(trace5, 3, 1)

trace6 = plotly.graph_objs.Scatter(
x=[i for i in range(6)],
y=[2 - i * 0.3 for i in range(6)],
name='',
showlegend=False,
)
fig.append_trace(trace6, 3, 2)

for i in range(1, 7):
fig['layout']['xaxis' + str(i)].update(range=row_range[((i - 1) // 2)][0])
fig['layout']['yaxis' + str(i)].update(range=row_range[((i - 1) // 2)][1])

plot_url = plotly.plotly.plot(fig)

关于python - 如何使用Python在plotly中标记子图a、b、c、d?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044790/

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