控制x ticks日期值 [英] controling the x ticks date values

查看:178
本文介绍了控制x ticks日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据样本为x,y对,x和y都为Unix时间戳:

  1354648326 ,1354648326 
1354649456,1371775551
1354649664,1429649819
1354649667,1429644021
1354649683,1356976159
1354649767,1441369794
1354649863,1414467362
1354650486, 1366297316
1354650757,1456962664
1354650789,1359398128
1354651552,1354656458
1354651555,1368631443
1354651591,1456420412
1354651616,1354651616
1354651715,1444573208
1354652048,1454443352
1354652382,1394722546
1354652687,1355993864
1354653448,1387378662
1354653731,1396094300
1354653769,1417765024
1354654110,1457230519
1354654111,1452854788
1354654179,1423877890
1354654266,1355148505
1354654374,1446848232
1354654374,1456864004
1354654615,1355858928
1354654700,1456945892
1354654707,1456265183
1354654744,1442939141
1354654747,1388436654
1354654771,1449799848
1354654775,1355177773
1354654808,1456857861
1354654809,1411369798
1354654855,1355934384
1354654915,1457100468
1354654962,1388784204
1354655085,1454446403
1354655219,1364196550
1354655232,1387214819
1354655262,1377170885
1354655264,1369689630
1354655289,1388750388
1354655389,1387387305
1354655434,1389255185
1354655436,1387165968
1354655592,1374369153
1354655661,1456912753
1354655811,1354718201
1354655889,1426675579
1354656139,1420486774

,我想将其绘制为散点,但没有在x和y轴上显示的丑陋的时间戳格式。
相反,我想在轴上绘制日期(格式为YYYY-MM-DD或任何其他可读格式),并显示3个月的差异。



我有以下代码:

  ax.set_xticklabels(getLabels(s,t),rotation = 20)

其中 getLabels(s,t)定义为: / p>

  def getLabels(s,t):#s和t是unix时间戳
labels = []
for x in pd.date_range(start = s,end = t,freq ='3M'):
labels.append(str(x).replace(00:00:00,))
打印标签
返回标签

并返回以下内容:

  ['2012-06-30','2012-09-30','2012-12-31','2013-03- 31,2013-06-30,2013-09-30,2013-12-31,2014-03-31,2014-06-30,2014-09-30 ,'2014-12-31','2015-03-31','2015-06-30','2015-09-30','2015-12-31','2016-03-31'] 
['2012- 06-30','2012-09-30','2012-12-31','2013-03-31','2013-06-30','2013-09-30','2013-12- 31,2014-03-31,2014-06-30,2014-09-30,2014-12-31,2015-03-31,2015-06-30 ,'2015-09-30','2015-12-31','2016-03-31']

现在,问题是x轴刻度标签的显示方式与前面的日期数组不一致,而只显示前6个日期(从2012-09-30开始,到2013年结束) -12-31)



有什么问题?

解决方案

你的问题是你的图只有五个刻度,所以只能显示五个标签。如果你想显示所有的标签,那么你需要确保你有相同的刻度数。



我没有安装大熊猫,无论如何,没有完整的数据,所以不能重新创建标签。我只是简单地复制了你提供的标签列表。我也'反向设计'最小&从标签的x轴的最大值(使数据在正确的位置)。



此行: ax.xaxis.set_ticks (np.arange(min_x,max_x,int((max_x-min_x)/ len(labels))))
确保您具有与标签相同数量的刻度。



请注意,我也更改了标签的水平对齐方式,即使压扁,仍然清楚标签对应的标记。数据的这一切显示在正确的位置,所以我很确定标签在正确的位置。



(显然y轴可以是以相同的方式处理)

  import matplotlib.pyplot as plt 
import numpy as np
import time
import datetime

labels = ['2012-06-30','2012-09-30','2012-12-31','2013-03-31',
'2013-06-30','2013-09-30','2013-12-31','2014-03-31',
'2014-06-30','2014- 09-30','2014-12-31','2015-03-31',
'2015-06-30','2015-09-30','2015-12-31' $ 3
x = []
y = []
with open('data.txt','r')as myfile:
for myline中的行:
_x,_y = line.strip()。split(',')
x.append(int(_x))
y.append(int(_y))

min_x = int(time.mktime(datetime.datetime.strptime('2012-06-30','%Y-%m-%d')。timetuple()))
max_x = in t(time.mktime(datetime.datetime.strptime('2016-03-31','%Y-%m-%d')。timetuple()))

print(datetime.datetime .fromtimestamp(min(x))。strftime('%Y-%m-%d'))
#确认我们正在绘制样本的正确位置

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(min_x,max_x)
ax.xaxis.set_ticks(np.arange(min_x,max_x ,int((max_x-min_x)/ len(labels))))
ax.set_xticklabels(labels,rotation = 20,horizo​​ntalalignment ='right')
ax.scatter(x,y)
plt.show()


I have the following data sample as x,y pairs and both x and y are Unix time-stamps:

1354648326,1354648326
1354649456,1371775551
1354649664,1429649819
1354649667,1429644021
1354649683,1356976159
1354649767,1441369794
1354649863,1414467362
1354650486,1366297316
1354650757,1456962664
1354650789,1359398128
1354651552,1354656458
1354651555,1368631443
1354651591,1456420412
1354651616,1354651616
1354651715,1444573208
1354652048,1454443352
1354652382,1394722546
1354652687,1355993864
1354653448,1387378662
1354653731,1396094300
1354653769,1417765024
1354654110,1457230519
1354654111,1452854788
1354654179,1423877890
1354654266,1355148505
1354654374,1446848232
1354654374,1456864004
1354654615,1355858928
1354654700,1456945892
1354654707,1456265183
1354654744,1442939141
1354654747,1388436654
1354654771,1449799848
1354654775,1355177773
1354654808,1456857861
1354654809,1411369798
1354654855,1355934384
1354654915,1457100468
1354654962,1388784204
1354655085,1454446403
1354655219,1364196550
1354655232,1387214819
1354655262,1377170885
1354655264,1369689630
1354655289,1388750388
1354655389,1387387305
1354655434,1389255185
1354655436,1387165968
1354655592,1374369153
1354655661,1456912753
1354655811,1354718201
1354655889,1426675579
1354656139,1420486774

and I want to plot it as scatter, but without the ugly time stamp format shown on x and y axis. Instead, I wanted to plot dates on the axis (in format YYYY-MM-DD or any other readable format) and show them with 3 months difference.

I have the following code:

ax.set_xticklabels(getLabels(s,t),rotation=20)

where getLabels(s,t) is defined as:

def getLabels(s,t): #s and t are unix time stamps
    labels =[]
    for x in pd.date_range(start=s, end=t, freq='3M'):
        labels.append(str(x).replace(" 00:00:00",""))
    print labels
    return labels

and returns something like:

['2012-06-30', '2012-09-30', '2012-12-31', '2013-03-31', '2013-06-30', '2013-09-30', '2013-12-31', '2014-03-31', '2014-06-30', '2014-09-30', '2014-12-31', '2015-03-31', '2015-06-30', '2015-09-30', '2015-12-31', '2016-03-31']
['2012-06-30', '2012-09-30', '2012-12-31', '2013-03-31', '2013-06-30', '2013-09-30', '2013-12-31', '2014-03-31', '2014-06-30', '2014-09-30', '2014-12-31', '2015-03-31', '2015-06-30', '2015-09-30', '2015-12-31', '2016-03-31']

Now, the problem is that the x axis ticks labels are not shown exactly as they are in the previous array of dates, instead, it shows only the first 6 dates (starting from 2012-09-30 and ending with 2013-12-31)

what is the problem?

解决方案

Your problem is that your graph only has five ticks, so it can only display five labels. If you want to display all the labels, then you need to make sure that you have the same number of ticks.

I don't have pandas installed, and anyway, don't have the full data so can't re-create the labels. I have simply copied the list of labels you have provided. I have also 'reverse-engineered' the min & max for the x-axis from the labels (so that the data plots in the right place).

This line: ax.xaxis.set_ticks(np.arange(min_x, max_x, int((max_x-min_x)/len(labels)))) Ensures that you have the same number of ticks as labels.

Note that I have also changed the horizontal alignment of the labels so that, even when squashed up, it is still clear which tick the label corresponds to. This slice of the data appears to plot in the right location, so I'm pretty sure the labels are in the right place.

(Obviously the y-axis can be treated in the same way)

import matplotlib.pyplot as plt
import numpy as np
import time
import datetime

labels =['2012-06-30', '2012-09-30', '2012-12-31', '2013-03-31',
         '2013-06-30', '2013-09-30', '2013-12-31', '2014-03-31',
         '2014-06-30', '2014-09-30', '2014-12-31', '2015-03-31',
         '2015-06-30', '2015-09-30', '2015-12-31', '2016-03-31']
x = []
y = []
with open('data.txt','r') as myfile:
    for line in myfile:
        _x, _y = line.strip().split(',')
        x.append(int(_x))
        y.append(int(_y))

min_x = int(time.mktime(datetime.datetime.strptime('2012-06-30','%Y-%m-%d').timetuple()))
max_x = int(time.mktime(datetime.datetime.strptime('2016-03-31','%Y-%m-%d').timetuple()))

print (datetime.datetime.fromtimestamp(min(x)).strftime('%Y-%m-%d')) 
# Confirm that we are plotting in the right place for this sample

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(min_x, max_x)
ax.xaxis.set_ticks(np.arange(min_x, max_x, int((max_x-min_x)/len(labels))))
ax.set_xticklabels(labels, rotation=20, horizontalalignment = 'right')
ax.scatter(x,y)
plt.show()

这篇关于控制x ticks日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆