Python 正在查找pyplot.plot()的标记文本选项
我正在寻找一种将数字或文本插入标记的方法。Python 正在查找pyplot.plot()的标记文本选项,python,text,matplotlib,plot,markers,Python,Text,Matplotlib,Plot,Markers,我正在寻找一种将数字或文本插入标记的方法。matplotlib.pyplot.plot(*args,**kwargs)文档中没有关于这方面的内容 默认的缩放级别将标记放置在边缘上,因此减少了可用于书写文本的可用空间 import matplotlib.pyplot as plt x = [1, 2, 3, 4 ,5] y = [1, 4, 9, 6, 10] plt.plot(x, y, 'ro',markersize=23) plt.show() 正如jkalden所建议的,annotate
matplotlib.pyplot.plot(*args,**kwargs)
文档中没有关于这方面的内容
默认的缩放级别将标记放置在边缘上,因此减少了可用于书写文本的可用空间
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
plt.plot(x, y, 'ro',markersize=23)
plt.show()
正如jkalden所建议的,
annotate
可以解决您的问题。函数的xy
-参数用于定位文本,以便将其放置在标记的位置上
关于“缩放”问题,matplotlib
默认情况下将在正在打印的最小值和最大值之间拉伸帧。这将导致外部标记的中心位于图形的边缘,并且只有一半标记可见。要覆盖默认的x和y限制,可以使用set_xlim
和set_ylim
。这里定义了一个偏移,用于控制边缘空间
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
fig, ax = plt.subplots()
# instanciate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)
## controls the extent of the plot.
offset = 1.0
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)
# loop through each x,y pair
for i,j in zip(x,y):
corr = -0.05 # adds a little correction to put annotation in marker's centrum
ax.annotate(str(j), xy=(i + corr, j + corr))
plt.show()
下面是它的外观:
您可以使用MathText执行此操作。 这是我们的说明书
这是对耍蛇人的方法的修正。我使用对齐选项(而不是手动偏移)将文本居中于点上,并使用其他选项设置文本的颜色、大小和粗体(重量)
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
fig, ax = plt.subplots()
# instanciate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)
## controls the extent of the plot.
offset = 1.0
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)
# loop through each x,y pair
for i,j in zip(x,y):
ax.annotate(str(j), xy=(i, j), color='white',
fontsize="large", weight='heavy',
horizontalalignment='center',
verticalalignment='center')
plt.show()
你所说的“由于较大的绘图和放大功能而不实用”是什么意思?你知道吗?最初的问题是向现有标记添加文本,在提问者的示例中,这些标记是点。你用简单的文字标记回答。有没有办法将两种标记类型合并为一种?或者点标记上的文本选项?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4 ,5]
y = [1, 4, 9, 6, 10]
fig, ax = plt.subplots()
# instanciate a figure and ax object
# annotate is a method that belongs to axes
ax.plot(x, y, 'ro',markersize=23)
## controls the extent of the plot.
offset = 1.0
ax.set_xlim(min(x)-offset, max(x)+ offset)
ax.set_ylim(min(y)-offset, max(y)+ offset)
# loop through each x,y pair
for i,j in zip(x,y):
ax.annotate(str(j), xy=(i, j), color='white',
fontsize="large", weight='heavy',
horizontalalignment='center',
verticalalignment='center')
plt.show()