Datetime-formatted rotated x-axis tick labels
您可以try 如下操作:
import numpy as np
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
### Sample data generation
date_rng = pd.date_range(start="2016-01-01", end="2016-01-02", freq="H")
temps = np.random.randint(17, 41, size=(len(date_rng)))
qc_values = np.ones(len(date_rng), dtype=int)
airtep_df = pd.DataFrame(
{"UTCTime": date_rng, "AIRTEP": temps, "AIRTEP_qc": qc_values}
)
print(airtep_df)
### Plotting
plt.figure(dpi=300)
plt.plot(airtep_df.UTCTime, airtep_df.AIRTEP, marker="o", linestyle="-")
# Rotating the x-tick labels
plt.xticks(rotation=45)
# Adjusting the date format
ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.tight_layout()
plt.xlabel("Time")
plt.ylabel("Temperature (℃)")
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()
这将产生:
UTCTime AIRTEP AIRTEP_qc
0 2016-01-01 00:00:00 30 1
1 2016-01-01 01:00:00 27 1
2 2016-01-01 02:00:00 37 1
3 2016-01-01 03:00:00 36 1
4 2016-01-01 04:00:00 18 1
5 2016-01-01 05:00:00 26 1
6 2016-01-01 06:00:00 27 1
7 2016-01-01 07:00:00 20 1
8 2016-01-01 08:00:00 27 1
9 2016-01-01 09:00:00 30 1
10 2016-01-01 10:00:00 37 1
11 2016-01-01 11:00:00 32 1
12 2016-01-01 12:00:00 29 1
13 2016-01-01 13:00:00 29 1
14 2016-01-01 14:00:00 23 1
15 2016-01-01 15:00:00 24 1
16 2016-01-01 16:00:00 37 1
17 2016-01-01 17:00:00 19 1
18 2016-01-01 18:00:00 19 1
19 2016-01-01 19:00:00 26 1
20 2016-01-01 20:00:00 35 1
21 2016-01-01 21:00:00 29 1
22 2016-01-01 22:00:00 24 1
23 2016-01-01 23:00:00 21 1
24 2016-01-02 00:00:00 39 1