Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

python - plot x-axis as date in matplotlib

I am trying to perform some analysis on data. I got csv file and I convert it into pandas dataframe. the data looks like this. Its has several columns, but I am trying to draw x-axis as date column. .

the pandas dataframe looks like this

print (df.head(10)

    cus-id        date       value_limit
0   10173         2011-06-12        455
1   95062         2011-09-11        455
2   171081        2011-07-05        212
3   122867        2011-08-18        123
4   107186        2011-11-23        334
5   171085        2011-09-02        376
6   169767        2011-07-03        34
7   80170         2011-03-23        34
8   154178        2011-10-02        34
9   3494          2011-01-01        34

I am trying to plot date data because there are multiple values for same date. for this purpose I am trying to plot x-asis ticks as date. since the minimum date in date column is 2011-01-01 and maximum date is 2012-04-20.

I tried something like this

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates

df = pd.read_csv('rio_data.csv', delimiter=',')
print (df.head(10))
d = []
for dat in df.date:
    # print (dat)
    d.append(datetime.strptime(df['date'], '%Y-%m-%d'))
days = dates.DayLocator()
datemin = datetime(2011, 1, 1)
datemax = datetime(2012, 4, 20) 
fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(days)
ax.set_xlim(datemin, datemax)
ax.set_ylabel('Count values')

But I am getting this error.

 AttributeError: 'DataFrame' object has no attribute 'date'

I am trying to draw date as x-axis, it should look like this. enter image description here

Can someone help me to draw the x-axis as date column. I would be grateful.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Set the index to the datetime series

If you set the index to the datetime series matplotlib will handle the x axis for you. Here is a minimal example of how you might deal with this visualization.

Simple example:

import pandas as pd
import matplotlib.pyplot as plt

date_time = ["2011-09-01", "2011-08-01", "2011-07-01", "2011-06-01", "2011-05-01"]
date_time = pd.to_datetime(date_time)
temp = [2, 4, 6, 4, 6]

DF = pd.DataFrame()
DF['temp'] = temp
DF = DF.set_index(date_time)

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.3)
plt.xticks(rotation=90)
plt.plot(DF)

This will yield a plot that looks like the following:

enter image description here

Setting the index makes things easier

The important note is that setting the DataFrame index to the datetime series allows matplotlib to deal with x axis on time series data without much help.

Follow this link for detailed explanation on spacing axis ticks (specifically dates)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...