简体   繁体   English

如何沿x轴均匀绘制日期数据?

[英]How to plot date data evenly along x-axis?

I am plotting data from a csv file with the values on the y-axis and the date on the x-axis. 我正在绘制csv文件中的数据,其中y轴上的值和x轴上的日期。 My dataset only includes data from June, July and August over a 15 year period. 我的数据集仅包括15年期间6月,7月和8月的数据。 However, when I try to plot this data, it plots all of the dates on the x-axis throughout the period rather than just the summer months in the csv file. 但是,当我尝试绘制这些数据时,它会在整个期间绘制x轴上的所有日期,而不仅仅是csv文件中的夏季月份。 Below is what my plot currently looks like 以下是我目前的情节

在此输入图像描述

Here is the code that produced this image: 以下是生成此图像的代码:

infile = r'data.csv'

with open(infile,'r') as f:
    data = list(reader(f))

date = [parser.parse(i[10]) for i in data[1:]] #3
date = mdates.date2num(date)
date = mdates.num2date(date)

value = [i[16] for i in data[1:]]

fig = plt.figure()
plt.plot(date, value, '.r')

Essentially, I am trying to get this same plot without all of the spaces between the each year's data. 基本上,我试图在没有每年数据之间的所有空格的情况下得到同样的情节。

Here is what snipet of my data looks like (with years from 2002-2016). 以下是我的数据的snipet(从2002年到2016年的数年)。 The date column (column L) consists of strings. 日期列(列L)由字符串组成。 This data is from a csv file just displayed in Excel. 此数据来自刚刚在Excel中显示的csv文件。 在此输入图像描述

I could imagine using as many subplots as there are date ranges could be an option. 我可以想象使用尽可能多的子图,因为日期范围可能是一个选项。 For simplicity, you may plot all data to all subplots, but limit each of the subplots to one date range. 为简单起见,您可以将所有数据绘制到所有子图,但将每个子图限制为一个日期范围。

import numpy as np; np.random.seed(24191)
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates

## generate some data x and y
n= 1000
year = np.random.randint(2000,2009, size=n)
month = np.random.randint(6,9, size=n)
day = np.random.randint(1,32, size=n)
x = [datetime.date(y,m,d) for y,m,d in zip(year,month,day)]
y = np.abs(np.random.randn(n))

## define the ranges for the dates
drange = [[datetime.date(i,6,1),datetime.date(i,8,31)] for i in range(2000,2009)]

## create as many subplots as there are date ranges
fig, axes= plt.subplots(ncols=len(drange), sharey=True)
fig.subplots_adjust(bottom=0.3,wspace=0)

ymax = 1.1*y.max()
## loop over subplots and limit each to one date range
for i, ax in enumerate(axes):
    ax.set_xlim(drange[i][0],drange[i][1])
    ax.set_ylim(0,ymax)
    ax.scatter(x,y, s=4)
    loc = matplotlib.dates.MonthLocator([6,7,8])
    fmt =  matplotlib.dates.DateFormatter("%Y-%b")
    ax.xaxis.set_major_locator(loc)
    ax.xaxis.set_major_formatter(fmt)
    plt.setp(ax.get_xticklabels(), rotation=90)
    if i!=0:
        ax.tick_params(axis="y", which="both", length=0)

plt.show()

在此输入图像描述

It sounds like you simply want to plot the data against a uniform array and then set the ticks to the dates, 听起来你只是想将数据绘制成一个统一的数组,然后将刻度设置为日期,

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

dates = ['06/2015','07/2015','08/2015', '06/2016','07/2016','08/2016']
x = [dt.datetime.strptime(d,'%m/%Y').date() for d in dates]
y = range(len(x)) + np.random.random(len(x))

#Plot vs dates
fig, ax = plt.subplots(2,1)
ax[0].plot(x,y,'r.')

#Plot vs number and label
ax[1].plot(y,'r.')
ax[1].set_xticks(range(len(y)))
ax[1].set_xticklabels(dates)
plt.show()

Which looks like this, 看起来像这样,

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM