How to change the step size used by matplotlib when building timestamp objects?

I'm currently trying to build a fairly small dataset using the matplotlib and pandas libraries. The dataset format is a CSV file. Here is the dataset:

DATE,UNRATE
1948-01-01,3.4
1948-02-01,3.8
1948-03-01,4.0
1948-04-01,3.9
1948-05-01,3.5
1948-06-01,3.6
1948-07-01,3.6
1948-08-01,3.9
1948-09-01,3.8
1948-10-01,3.7
1948-11-01,3.8
1948-12-01,4.0

I uploaded the dataset using pandas (as you can see, the file that contains this dataset is called "dataset.csv"):

import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('dataset.csv')
dataset['DATE'] = pd.to_datetime(dataset['DATE'])

Then I tried to build a loaded dataset using matplotlib:

plt.plot(dataset['DATE'], dataset['UNRATE'])
plt.show()

The above code basically worked fine and displayed the following graph:

enter image description here

However, the problem is that the data that I wanted to display on the x axis was apparently only built with an interval of two:

enter image description here

, "" x y matplotlib?, . , , , .

, x pyplot, , , . , , , to_pydatetime, DatetimeIndex. , pandas.to_datetime DatetimeIndex , to_pydatetime dataset['DATE']:

plt.xticks(dataset['DATE'].to_pydatetime())

:

AttributeError: 'Series' object has no attribute 'to_pydatetime'

, -, , matplotlib x, ?

+4
1

, , :

plt.xticks(dataset['DATE'].tolist(),dataset['DATE'].tolist())

plt.xticks(dataset['DATE'].dt.to_pydatetime(),dataset['DATE'].dt.to_pydatetime()) 

enter image description here

.

, , .. .

import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('dateunrate.txt')
plt.plot(dataset['DATE'], dataset['UNRATE'])

plt.setp(plt.gca().get_xticklabels(), rotation=45, ha="right")
plt.show()

enter image description here

plt.plot(["apple", "banana", "cherry"], [1,2,3]). , , , , ​​ . . 2018-01-01, 2018-01-03, 2018-01-27, .

pandas ()

Pandas , . , csv .

import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('dateunrate.txt', parse_dates=[0], index_col=0)
dataset.plot()

plt.show() 

enter image description here

dataset = pd.read_csv('../dateunrate.txt', parse_dates=[0])
dataset = dataset.set_index("DATE")
dataset.plot()

dataset = pd.read_csv('../dateunrate.txt')
dataset["DATE"] = pd.to_datetime(dataset["DATE"])
dataset = dataset.set_index("DATE")
dataset.plot()

dataset = pd.read_csv('../dateunrate.txt')
dataset["DATE"] = pd.to_datetime(dataset["DATE"])
dataset.plot(x="DATE",y="UNRATE")

, , pandas 12 .
.

matplotlib pandas ()

matplotlib.dates formatters locators () , . MonthLocator "%b %Y". matplotlib plot pandas plot(x_compat=True).

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates

dataset = pd.read_csv('dateunrate.txt', parse_dates=[0], index_col=0)

plt.plot(dataset.index, dataset['UNRATE'])
## or use 
#dataset.plot(x_compat=True) #note the x_compat argument

plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))

plt.setp(plt.gca().get_xticklabels(), rotation=45, ha="right")
plt.show()

enter image description here

+2

Source: https://habr.com/ru/post/1695295/


All Articles