gpt4 book ai didi

python - 如何将 pylab.plot 线放在 pylab.figtext 上?

转载 作者:行者123 更新时间:2023-12-01 04:54:24 26 4
gpt4 key购买 nike

我希望情节线覆盖文本。这是当前代码:

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()

enter image description here

下面是当我们更改 zorder 时会发生的情况(如果我没记错的话,文本的默认值是 3。任何大于或等于 4 的 zorder 都会覆盖任何文本图中的对象。)

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot(range(10), color='red', lw=3, zorder=4)
# Placing the text in data coordinates...
ax.text(5, 5, 'Test', color='white', fontsize=45,
bbox={'facecolor':'black', 'pad':12})

plt.show()

enter image description here

但是,如果我们使用 plt.figtextfig.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()

enter image description here

因此,如果您想使用图形坐标并且仍然让线条位于顶部,则应该使用 ax.textax.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()

enter image description here

关于python - 如何将 pylab.plot 线放在 pylab.figtext 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756620/

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