99 python plot matplotlib
如何将文本放在matplotlib图的左上角(或右上角),例如左上角的图例,或者在图的顶部,但在左上角?例如,如果它是plt.scatter(),那么将在散射的正方形内的东西放在最左上角.
我想在没有理想地知道绘制的散点图的比例的情况下这样做,因为它将从数据集更改为数据集.我只是希望文本大致位于左上角,或大致位于右上角.对于图例类型定位,它不应与任何散点图点重叠.
谢谢!
joa*_*uin 142
你可以用text
.
text(x, y, s, fontsize=12)
Run Code Online (Sandbox Code Playgroud)
text
坐标可以相对于轴给出,因此文本的位置将独立于图的大小:
默认转换指定文本在数据坐标中,或者,您可以在轴坐标中指定文本(0,0是左下角,1,1是右上角).下面的例子将文本放在轴的中心::
text(0.5, 0.5,'matplotlib',
horizontalalignment='center',
verticalalignment='center',
transform = ax.transAxes)
Run Code Online (Sandbox Code Playgroud)
防止文本干扰散射的任何一点是更困难的事情.更简单的方法是将y_axis(ymax in ylim((ymin,ymax))
)设置为比点的最大y坐标高一点的值.通过这种方式,您将始终拥有文本的可用空间.
编辑:这里有一个例子:
In [17]: from pylab import figure, text, scatter, show
In [18]: f = figure()
In [19]: ax = f.add_subplot(111)
In [20]: scatter([3,5,2,6,8],[5,3,2,1,5])
Out[20]: <matplotlib.collections.CircleCollection object at 0x0000000007439A90>
In [21]: text(0.1, 0.9,'matplotlib', ha='center', va='center', transform=ax.transAxes)
Out[21]: <matplotlib.text.Text object at 0x0000000007415B38>
In [22]:
Run Code Online (Sandbox Code Playgroud)
ha和va参数设置文本相对于插入点的对齐方式.即.ha ='left'是一个很好的设置,可以在手动缩小(缩小)帧时防止长文本离开左轴.
Tre*_*ney 12
matplotlib
与发布原始答案时有些不同matplotlib.pyplot.text
matplotlib.axes.Axes.text
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 6))
plt.text(0.1, 0.9, 'text', size=15, color='purple')
# or
fig, axe = plt.subplots(figsize=(6, 6))
axe.text(0.1, 0.9, 'text', size=15, color='purple')
Run Code Online (Sandbox Code Playgroud)
import matplotlib.pyplot as plt
# Build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
ax = plt.gca()
p = plt.Rectangle((left, bottom), width, height, fill=False)
p.set_transform(ax.transAxes)
p.set_clip_on(False)
ax.add_patch(p)
ax.text(left, bottom, 'left top',
horizontalalignment='left',
verticalalignment='top',
transform=ax.transAxes)
ax.text(left, bottom, 'left bottom',
horizontalalignment='left',
verticalalignment='bottom',
transform=ax.transAxes)
ax.text(right, top, 'right bottom',
horizontalalignment='right',
verticalalignment='bottom',
transform=ax.transAxes)
ax.text(right, top, 'right top',
horizontalalignment='right',
verticalalignment='top',
transform=ax.transAxes)
ax.text(right, bottom, 'center top',
horizontalalignment='center',
verticalalignment='top',
transform=ax.transAxes)
ax.text(left, 0.5 * (bottom + top), 'right center',
horizontalalignment='right',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(left, 0.5 * (bottom + top), 'left center',
horizontalalignment='left',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',
horizontalalignment='center',
verticalalignment='center',
transform=ax.transAxes)
ax.text(right, 0.5 * (bottom + top), 'centered',
horizontalalignment='center',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(left, top, 'rotated\nwith newlines',
horizontalalignment='center',
verticalalignment='center',
rotation=45,
transform=ax.transAxes)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
一种解决方案是使用该plt.legend
功能,即使您不想要实际的图例.您可以使用loc
keyterm 指定图例框的位置.更多信息可以在这个网站上找到,但我还提供了一个展示如何放置图例的示例:
ax.scatter(xa,ya, marker='o', s=20, c="lightgreen", alpha=0.9)
ax.scatter(xb,yb, marker='o', s=20, c="dodgerblue", alpha=0.9)
ax.scatter(xc,yc marker='o', s=20, c="firebrick", alpha=1.0)
ax.scatter(xd,xd,xd, marker='o', s=20, c="goldenrod", alpha=0.9)
line1 = Line2D(range(10), range(10), marker='o', color="goldenrod")
line2 = Line2D(range(10), range(10), marker='o',color="firebrick")
line3 = Line2D(range(10), range(10), marker='o',color="lightgreen")
line4 = Line2D(range(10), range(10), marker='o',color="dodgerblue")
plt.legend((line1,line2,line3, line4),('line1','line2', 'line3', 'line4'),numpoints=1, loc=2)
Run Code Online (Sandbox Code Playgroud)
请注意,因为loc=2
图例位于图的左上角.如果文本与绘图重叠,您可以通过使用使其变小legend.fontsize
,这将使图例变小.
归档时间: |
|
查看次数: |
140314 次 |
最近记录: |