简体   繁体   English

如何在python中有选择地绘制数据

[英]how to plot data selectively in python

I'm trying to do some data analyzing using python and I have got the data displayed successfully. 我正在尝试使用python做一些数据分析,并且我已经成功显示了数据。 But there is a problem with the figure. 但是这个数字有问题。 在此处输入图片说明

As you can see, the x-axis(date) is quite messed up. 如您所见,x轴(日期)非常混乱。 The reason is that the data format is like(collected on daily-basis): 原因是数据格式类似于(按日收集):

2014-12-30,1423.98,1424.16,1407.51,1407.51
2014-12-29,1433.80,1433.84,1409.53,1424.67
...

I put the first row(dates) in an array and used the array as x-axis. 我将第一行(日期)放入数组中,并将该数组用作x轴。

I have a solution myself, which is to only output x-axis when the month changes(like '2014-11', '2014-12',...), but I don't know any function that can do this... 我自己有一个解决方案,它只在月份更改时才输出x轴(例如“ 2014-11”,“ 2014-12”等),但我不知道有任何功能可以执行此操作。 ..

Can anyone help me with my problem? 谁能帮我解决我的问题? Thanks! 谢谢!

There's actually quite a bit of examples out there on this topic already I'd wager. 我已经下注了,实际上有很多关于该主题的示例。 It kind of depends on how exactly do you get your dates (which format etc..). 这取决于您如何准确地获取日期(格式等)。

If you're not doing something overly out of this world and settle with the datetime module you can take a look at the example bellow. 如果您没有在这个世界上做过多的事情,并且使用datetime模块解决了问题,那么可以看一下下面的示例。 Fair warning from personal experience, locators and formatters are susceptible to weirdest bugs once the number of ticks reaches a very high number. 从个人经验,定位器和格式化程序发出的合理警告会在刻度数达到很高数量时易受最奇怪的错误影响。

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from datetime import date, timedelta

#####################################
###  Creating data for plot, 
###  PLEASE include this in your next question.
#####################################
times = []
startdate = date(2014, 1, 1)
enddate = date(2016, 12, 22)
while startdate < enddate:
    times.append(startdate)
    startdate += timedelta(days=1) 

x = []
for i in range(0, len(times)):
    x.append(np.random.random(10))

First example, per months and days. 第一个示例,每月和每天。 The ticks for days are there, I just haven't shown them on the graph. 几天的滴答声就在那里,我只是没有在图表上显示出来。

months = mpl.dates.MonthLocator()
days = mpl.dates.DayLocator(1) #find every 1st day of month
monthsFormatter = mpl.dates.DateFormatter('%b')

fig, ax = plt.subplots()

ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFormatter)
ax.xaxis.set_minor_locator(days)

ax.plot(times, x)

plt.show()

月和日定位器

Second example is something like this, per year basis with a more "detailed" formatter. 第二个例子是这样的,每年以更“详细”的格式化程序为基础。

years = mpl.dates.YearLocator()
months = mpl.dates.MonthLocator()
yearsFormatter = mpl.dates.DateFormatter('%Y:%b')

fig, ax = plt.subplots()

ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFormatter)
ax.xaxis.set_minor_locator(months)

ax.plot(times, x)

plt.show()

which looks something like: 看起来像这样:

年和月定位器,带有格式化程序

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

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