简体   繁体   中英

How adjust picture size to show dates on x axis

I am working on the following dataset (actually the following ones are just the first ten observations)

Update  Pb95    Pb98    diesel  heating oil
0   6/2/2022    7519    8311    7172    5582
1   6/1/2022    7406    8194    6912    5433
2   5/31/2022   7213    7950    6754    5394
3   5/28/2022   7129    7864    6711    5360
4   5/27/2022   7076    7798    6704    5366
5   5/26/2022   6895    7504    6502    5182
6   5/25/2022   6714    7306    6421    5130
7   5/24/2022   6770    7358    6405    5153
8   5/21/2022   6822    7421    6457    5216
9   5/20/2022   6826    7430    6523    5281

I have tried plotting them by using the following code:

import matplotlib.pyplot as plt

plt.plot(df['Update'], df['Pb95'], label='sales', linewidth=3) 

#add title and axis labels
plt.title('Sales by Date')
plt.xlabel('Date')
plt.ylabel('Sales')

#add legend
plt.legend()

#display plot
plt.show() 

By obtaining the following picture

在此处输入图像描述

As you could see, dates are reported overlapped. Could you please suggest a possible solution to make this picture clearer (by rotating them 45 degrees, for instance, or other solutions)?

Thanks

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


fig, ax = plt.subplots()
ax.plot(df['Update'], df['Pb95'], label='sales', linewidth=3)
locator = matplotlib.ticker.LinearLocator(4)
ax.xaxis.set_major_locator(locator)
fig.autofmt_xdate(rotation=45)

plt.title('Sales by Date')
plt.xlabel('Date')
plt.ylabel('Sales')

plt.legend()

plt.show()

Uses locator to show four values on the x-axis. The autofmt_xdate function is used to set the angle to 45 of these values. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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