当matplotlob,x轴的输入为datatime64的数组时,如何控制其横坐标标签只显示时分秒,而非日期,
时间: 2024-05-09 17:18:49 浏览: 20
可以使用matplotlib库中的DateFormatter类来控制横坐标标签的显示格式。具体操作如下:
```python
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import datetime as dt
# 生成一组时间序列数据
x = np.arange('2021-01-01T00:00:00', '2021-01-02T00:00:00', np.timedelta64(1, 'm'), dtype='datetime64')
# 生成对应的随机数据
y = np.random.rand(len(x))
# 创建图像窗口
fig, ax = plt.subplots()
# 绘制折线图
ax.plot(x, y)
# 设置x轴标签格式
date_fmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(date_fmt)
ax.xaxis.set_minor_formatter(date_fmt)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
# 显示图像
plt.show()
```
在上述代码中,我们使用了mdates.DateFormatter类来创建一个只显示时分秒的时间格式。然后通过ax.xaxis.set_major_formatter()和ax.xaxis.set_minor_formatter()方法来设置横坐标标签的主副格式。最后,通过ax.xaxis.set_tick_params()方法来设置标签的旋转角度和字体大小。运行代码后,会得到一个只显示时分秒的折线图。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)