Matplotlib修复轴-日期过多 [英] Matplotlib fix axis - too many dates

查看:74
本文介绍了Matplotlib修复轴-日期过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量数据作为csv文件,具有太多的日期,所以当我绘制它时,x轴会全部写入它们,例如fe:从 2000-12-24 2017-12-24 以及y轴.

I have big data as csv file which has too many dates, so when I plot it, x axis writes all of them, like f.e : from 2000-12-24 to 2017-12-24 and also y axis.

我尝试使用一个集合,但是该集合需要排序,问题是当我对其进行排序时,来自Y的数据不适用于任何排序日期.

I have tried to use a set, but that set needs to sort and problem is that when I sort it the data from Y isn't for any of sorted dates.

import matplotlib.pyplot as plt
import urllib as u
import numpy as np
import csv

stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'

date = []
openp = []
high = []
low = []
close = []
adjclose = []
volume = []

text = u.request.urlopen(stock_price_url).read().decode()
with open('nw.csv', 'w') as fw:
    fw.write(text)
    fw.close()

with open('nw.csv', 'r') as csvf:
f = csv.reader(csvf, delimiter=',')
for row in f:
    if 'Date' not in row:
        date.append(row[0])
        openp.append(row[1])
        high.append(row[2])
        low.append(row[3])
        close.append(row[4])
        adjclose.append(row[5])
        volume.append(row[6])

dateset = set([])            
for z in date:
   dateset.add(z[:4])

highset = []
for z in high:
    highset.append(z[:3])


plt.plot(set(dateset), set(highset), linewidth=0.5)
plt.show()

推荐答案

您需要先将日期转换为Python datetime 对象.然后可以将其转换为matplotlib号.然后,您可以告诉matplotlib根据年份或月份的变化添加刻度线:

You need to convert the dates first into a Python datetime object. This can then be converted into a matplotlib number. With this you can then tell matplotlib to add ticks based on year or month changes:

from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import urllib as u
import numpy as np
import csv

stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'

date = []
high = []

text = u.request.urlopen(stock_price_url).read().decode()

with open('nw.csv', 'w') as f_nw:
    f_nw.write(text)

with open('nw.csv', 'r', newline='') as f_nw:
    csv_nw = csv.reader(f_nw)
    header = next(csv_nw)

    for row in csv_nw:
        date.append(matplotlib.dates.date2num(datetime.strptime(row[0], '%Y-%m-%d')))
        high.append(row[2])

ax = plt.gca()
#ax.xaxis.set_minor_locator(matplotlib.dates.MonthLocator([1, 7]))
#ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y'))
#ax.tick_params(pad=20)

plt.plot(date, high, linewidth=0.5)
plt.show()   

这只会给您几年:

或者,如果您取消注释次要定位器/格式化程序,则会得到:

Or if you uncomment the minor locator/formatter you would get:

注意:

  1. 如果使用 with 块打开文件,则无需关闭文件.

  1. You do not need to close a file if you are opening it with a with block.

脚本假定您正在使用Python 3.x.

The script assumes you are using Python 3.x.

要跳过标头,只需在遍历for循环中的行之前使用 next()读取标头即可.

To skip the header just read it in using next() before iterating over the rows in your for loop.

这篇关于Matplotlib修复轴-日期过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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