gla*_*yne 6 python csv plot matplotlib
我正在使用y轴上的值和x轴上的日期来绘制csv文件中的数据.我的数据集仅包括15年期间6月,7月和8月的数据.但是,当我尝试绘制这些数据时,它会在整个期间绘制x轴上的所有日期,而不仅仅是csv文件中的夏季月份.以下是我目前的情节
以下是生成此图像的代码:
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')
Run Code Online (Sandbox Code Playgroud)
基本上,我试图在没有每年数据之间的所有空格的情况下得到同样的情节.
以下是我的数据的snipet(从2002年到2016年的数年).日期列(列L)由字符串组成.此数据来自刚刚在Excel中显示的csv文件.
我可以想象使用与日期范围一样多的子图可能是一种选择。为简单起见,您可以将所有数据绘制到所有子图,但将每个子图限制为一个日期范围。
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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2243 次 |
最近记录: |