使用自定义文本绘制 x 轴点 [英] plot with custom text for x axis points
问题描述
我正在使用 matplotlib 和 python 绘制图,如下面的示例代码.
I am drawing a plot using matplotlib and python like the sample code below.
x = array([0,1,2,3])
y = array([20,21,22,23])
plot(x,y)
show()
因为是上面 x 轴上的代码,我将看到绘制的值 0.0, 0.5, 1.0, 1.5
,即与我的参考 x 值相同的值.
As it is the code above on the x axis I will see drawn values 0.0, 0.5, 1.0, 1.5
i.e. the same values of my reference x values.
有没有将 x 的每个点映射到不同的字符串?例如,我希望 x 轴显示月份名称(字符串 Jun, July,...
)或其他字符串,如人名("John", "Arnold", ...
) 或时钟时间 ( "12:20", "12:21", "12:22", ..
).
Is there anyway to map each point of x to a different string? So for example I want x axis to show months names( strings Jun, July,...
) or other strings like people names ( "John", "Arnold", ...
) or clock time ( "12:20", "12:21", "12:22", ..
).
你知道我可以做什么或看看什么功能吗?
为了我的目的,它可能是 matplotlib.ticker
的帮助吗?
Do you know what I can do or what function to have a look at?
For my purpose could it be matplotlib.ticker
of help?
推荐答案
您可以使用 pyplot.xticks:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,1,2,3])
y = np.array([20,21,22,23])
my_xticks = ['John','Arnold','Mavis','Matt']
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()
这篇关于使用自定义文本绘制 x 轴点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!