[英]Python Matplotlib scatter plot adding x-axis labels
I have this following code in order to generate scatterplots 我有以下代码以生成散点图
import matplotlib.pyplot as plt
line = plt.figure()
plt.plot(xvalue, yvalue)
plt.grid(True)
plt.savefig("test.png")
plt.show()
and here is the screenshot of the plot: 这是剧情的屏幕截图:
I am just wondering if i could change the x-axis labels into strings. 我只是想知道我是否可以将x轴标签更改为字符串。 I have stored all the labels in
我已将所有标签存储在
xlabel = ['2015/4/1', '2015/4/11', '2015/4/12', '2015/4/18', '2015/4/19'...]
Is there any function for matplotlib so that i could set x-axis labels to the values in "xlabel"? matplotlib是否有任何功能,以便我可以将x轴标签设置为“ xlabel”中的值?
many thx! 多谢!
ALso my labels are overlapped, anything i could do to fix this problem? 我的标签也重叠了,我能做些什么来解决这个问题? thx!
谢谢!
Here is my answer. 这是我的答案。 You target was to plot the datetime as xticklabel.
您的目标是将日期时间绘制为xticklabel。
I always do something like this. 我总是做这样的事情。 Code like this:
像这样的代码:
## For example, I have 9 daily value during 2013-04-01 to 2014-04-10
start = datetime.datetime.strptime("01-04-2013", "%d-%m-%Y")
end = datetime.datetime.strptime("10-04-2013", "%d-%m-%Y")
date = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
plt.figure(figsize=(12,4))
## y is the data I want to plot
ind = np.arange(len(y))
ax=plt.subplot()
plt.plot(ind,y,lw = 3)
k = []
for i in range(0,len(ind),1):
k.append(str(date[i])[0:10])
plt.xticks(ind,k,rotation=65)
To solve the overlap problem, I recommend the code below: 为了解决重叠问题,我建议使用以下代码:
for label in ax.xaxis.get_ticklabels()[::2]:
label.set_visible(False)
For daily value in a month, you can get a figure like this: 对于一个月内的每日价值,您可以得到如下图:
Do: 做:
plt.xticks(xs, labels)
Where xs
is a list of the positions for the ticks, and labels
is the list of labels. 其中
xs
是刻度的位置列表,而labels
是标签列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.