如何使用 Python 标记子图 a、b、c、d?
How can I label subplots a,b,c,d in plotly using Python?
我想标记子图,类似于:
无论图的比例如何,标签 (a)、(b) 等在图上的相同位置。换句话说,似乎一个很好的方式来指定它是在图上以英寸或厘米为单位的位置。我如何在 Python 中使用 plotly 执行此操作?我只见过 plotting scatter points 注释的方法,这似乎很难在子图中实现完全不同的比例。
您可以通过 layout: annotations
注释您的子图。将 xref
和 yref
设置为 paper
可让您指定相对于总绘图大小的 x
和 y
坐标(箭头也需要通过 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
)))
该图是使用以下代码创建的。它肯定需要更多调整,但你会明白的。
剧情还需要一些小东西:
- 通过
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)
我想标记子图,类似于:
无论图的比例如何,标签 (a)、(b) 等在图上的相同位置。换句话说,似乎一个很好的方式来指定它是在图上以英寸或厘米为单位的位置。我如何在 Python 中使用 plotly 执行此操作?我只见过 plotting scatter points 注释的方法,这似乎很难在子图中实现完全不同的比例。
您可以通过 layout: annotations
注释您的子图。将 xref
和 yref
设置为 paper
可让您指定相对于总绘图大小的 x
和 y
坐标(箭头也需要通过 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
)))
该图是使用以下代码创建的。它肯定需要更多调整,但你会明白的。
- 通过
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)