Python 如何在pylab.figtext上放置pylab.plot线?
我希望打印线覆盖文本。以下是当前代码:Python 如何在pylab.figtext上放置pylab.plot线?,python,matplotlib,plot,Python,Matplotlib,Plot,我希望打印线覆盖文本。以下是当前代码: pylab.plot( data[:,0], data[:,1], color='red', lw=3, label='Temp' ) pylab.figtext(0.7, 0.5, round(temp1,2), color='white', fontsize=450, bbox={'facecolor':'black', 'alpha':0.8, 'pad':12}) 我使用了非常大的白色数字,红色的绘图线被这些数字隐藏了。我只是希望绘图线位
pylab.plot( data[:,0], data[:,1], color='red', lw=3, label='Temp' )
pylab.figtext(0.7, 0.5, round(temp1,2), color='white', fontsize=450,
bbox={'facecolor':'black', 'alpha':0.8, 'pad':12})
我使用了非常大的白色数字,红色的绘图线被这些数字隐藏了。我只是希望绘图线位于数字的顶部。我尝试将alpha=0.2添加到figtext行,然后我可以看到绘图行,但白色数字和绘图行看起来都很暗。使用
zorder
kwarg来控制交叉点
默认情况下,文本比线条、图像和其他打印特征具有更高的zorder
(其思想是文本通常是应该位于顶部的标签)
然而,还有一个额外的皱纹。您当前正在使用figtext
。这会将文本放置在地物级别而不是轴级别。您需要使用plt.text
/ax.text
。如果要按当前的状态指定地物坐标中的位置,可以传入transform=fig.transFigure
下面是一个默认情况下的示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), color='red', lw=3)
# Placing the text in data coordinates...
ax.text(5, 5, 'Test', color='white', fontsize=45,
bbox={'facecolor':'black', 'pad':12})
plt.show()
下面是当我们更改zorder
时发生的情况(如果我没记错的话,文本的默认值是3。大于或等于4的任何zorder
都会覆盖绘图中的任何文本对象。)
但是,如果我们使用plt.figtext
或fig.text
(它们是相同的),文本将放置在图形级别而不是轴级别。因此,无论行的zorder
是什么,文本仍将位于轴中任何内容的前面
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), color='red', lw=3, zorder=5000)
# Placing the text in figure coordinates
fig.text(0.5, 0.5, 'Test', color='white', fontsize=45,
bbox={'facecolor':'black', 'pad':12})
plt.show()
因此,如果您想使用地物坐标,且线条仍在顶部,则应使用ax.text
或ax.annotate
并指定transform=fig.transFigure
,以便将您使用的坐标解释为地物分数:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), color='red', lw=3, zorder=4)
# Placing the text in figure coordinates
ax.text(0.5, 0.5, 'Test', color='white', fontsize=45, transform=fig.transFigure,
bbox={'facecolor':'black', 'pad':12})
plt.show()
为什么不使用轴艺术家的
注释
或文本
,而不是人物级艺术家的figtext
?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), color='red', lw=3, zorder=4)
# Placing the text in figure coordinates
ax.text(0.5, 0.5, 'Test', color='white', fontsize=45, transform=fig.transFigure,
bbox={'facecolor':'black', 'pad':12})
plt.show()