在python图中添加额外信息?

时间:2013-04-17 17:45:54

标签: python matplotlib

假设我们有一个带有三个图的图,用于三个不同的参数。但对于所有三个图我们有相同的温度T = 4K。那我怎么能在图中添加这些信息呢?

我不想在标题中写下来。我希望它在数字本身上。

5 个答案:

答案 0 :(得分:4)

figtext 会很好用。

figtext优于textannotate的优点是figtext默认使用数字坐标,而其他默认使用轴的坐标(和因此,如果你的轴在不同的图表之间不同,“T = 4K”会移动。)

import matplotlib.pyplot as plt

plt.figure()
plt.xlim(-10, 10)
plt.ylim(0, .01)
plt.figtext(.8, .8, "T = 4K")
plt.show()

enter image description here

答案 1 :(得分:2)

嗯,我不确定您的意思,但您可以使用text()方法向情节添加文字。

Plot text in matplotlib pyplot

答案 2 :(得分:1)

以下是使用annotate的演示。查看this example以了解不同样式的注释。

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
fig, ax = plt.subplots()

x = np.linspace(0,4,100)

plt.plot(x,2*x)
plt.plot(x,x**2)
plt.plot(x,np.sqrt(8*x))

ax.annotate('T = 4K', xy=(2,4), xycoords='data',
            xytext=(-100,60), textcoords='offset points',
            arrowprops=dict(arrowstyle='fancy',fc='0.6',
                            connectionstyle="angle3,angleA=0,angleB=-90"))

plt.show()
raw_input()

enter image description here

答案 3 :(得分:1)

figtext可以在多个子图的基础上进行注释,就像一个独立于数字的注释,因此您可以在一张图片中进行额外的注释或备注。我也在寻找这个。感谢你们! : - )

import matplotlib.pyplot as plt

plt.figure(1)
plt.suptitle("SOME TITLE HERE")
#FIRST SUBPLOT
plt.subplot(311)
plt.ylabel(r"$a [m/s^2]$") # YOU CAN USE LaTeX TYPESETTING IN PYPLOT STRINGS!
plt.xlabel("time [s]")
plt.grid(True)
plt.plot(some_data)
# SECOND SUBPLOT
plt.subplot(312)
...
# THIRD SUBPLOT
plt.subplot(313)
...
# BOTTOM LABEL
plt.figtext(0.5, 0, "SOME LABEL BELOW ALL SUBPLOTS", ha="center", fontsize=7, bbox={"facecolor":"orange", "alpha":0.5, "pad":5})
# DRAW THE PLOT
plt.show()

Notre ha=center会将字符串置于x=0.5的中心位置。您还可以使用fontsizebbox参数来更改字符串及其区域的外观。

答案 4 :(得分:0)

我建议在T = 4K区域周围有一个灰色的水平区域

如果你看一下matplotlib documentation for axes中的axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs),就可以做出类似的事情:

horizontal and vertical zones