我有一个名为airtep_df的DataFrame,其列为'AIRTEP'(Int64)、'AIRTEP_qc'(Int64)和'UTCTime'.我正在try 从这些数据创建一个曲线图,但x轴上的时间刻度标签重叠,使曲线图不可读.我try 使用plt.xticks(rotation=45)旋转刻度标签,但绘图消失了.谁能解释一下哪里出了问题,并提供正确格式化datetime时间刻度标签的解决方案?

此图显示了我正在使用的数据框的详细信息:

enter image description here

这是我使用的脚本:

import matplotlib.pyplot as plt
import pandas as pd


index = (airtep_df['AIRTEP'] != 9999) & (airtep_df['AIRTEP_qc'] == 1)
selected_y = airtep_df.loc[index,'AIRTEP']
y = selected_y.values
selected_x = time_df.loc[index,'UTCTime']
x = selected_x.values
plt.plot(x[30],y[30])

#This is the result I have, the the x-axis are overlapping:enter image description here

#然后我试着旋转xtick

plt.xticks(rotation=45)

这是我得到的结果,如图所示:

enter image description here

我如何绘制datetime个数据并将其格式化以使标签不重叠?

推荐答案

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

Matplotlib plot with custom formatted datetime x-axis tick labels

Python相关问答推荐

aiohTTP与pytest的奇怪行为

Tokenizer Docker:无法为Tokenizer构建轮子,这是安装pyproject.toml项目所需的

如何处理必须存在于环境中但无法安装的Python项目依赖项?

如果我已经使用了time,如何要求Python在12秒后执行另一个操作.sleep

基本链合同的地址是如何计算的?

symy.分段使用numpy数组

opencv Python稳定的图标识别

Python plt.text中重叠,包adjust_text不起作用,如何修复?

优化在numpy数组中非零值周围创建缓冲区的函数的性能

Locust请求中的Python和参数

使用Keras的线性回归参数估计

未删除映射表的行

2D空间中的反旋算法

为什么默认情况下所有Python类都是可调用的?

Telethon加入私有频道

Python键入协议默认值

从spaCy的句子中提取日期

Polars asof在下一个可用日期加入

如何在TensorFlow中分类多个类

重置PD帧中的值