Matplotlib x轴设置间隔

使用默认设置绘制折线图

import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2)

values1 = [
    [32, 15, 22],
    [28, 31, 33],
    [27,26,29],
    [22,25,23]
]
values2 = [
    [65,60,62],
    [57,50,53],
    [35,38,36],
    [38,40,37]
]
names = [
    "chuku","zhixian","zhuanwan","diaotou"
]

for i in range(2):
    for j in range(2):
        ax[i][j].plot( [1,2,3], values1[2*i+j], [1,2,3], values2[2*i+j])
        ax[i][j].set_title(names[2*i+j])
        ax[i][j].set_xlabel("times")
        ax[i][j].set_ylabel("cm")
        ax[i][j].set_ylim(10, 70)
        ax[i][j].grid(True)

fig.legend(['20230505','1.5.1.0410'],loc='upper right')
plt.show()

效果:
Figure_1.png

设置间隔后的折线图

import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

x_major_locator=MultipleLocator(1) # 把刻度间隔设置为1,并存在变量中
fig, ax = plt.subplots(2, 2)

values1 = [
    [32, 15, 22],
    [28, 31, 33],
    [27,26,29],
    [22,25,23]
]
values2 = [
    [65,60,62],
    [57,50,53],
    [35,38,36],
    [38,40,37]
]
names = [
    "chuku","zhixian","zhuanwan","diaotou"
]

for i in range(2):
    for j in range(2):
        ax[i][j].plot( [1,2,3], values1[2*i+j], [1,2,3], values2[2*i+j])
        ax[i][j].set_title(names[2*i+j])
        ax[i][j].set_xlabel("times")
        ax[i][j].set_ylabel("cm")
        ax[i][j].set_ylim(10, 70)
        ax[i][j].xaxis.set_major_locator(x_major_locator) # 把x轴设置为1的倍数
        ax[i][j].grid(True)

fig.legend(['QA_20230505_daily','1.5.1.0410'],loc='upper right')
plt.show()

效果:
Figure_1.png

去除网格线后的效果

import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

x_major_locator=MultipleLocator(1)
fig, ax = plt.subplots(2, 2)

values1 = [
    [32, 15, 22],
    [28, 31, 33],
    [27,26,29],
    [22,25,23]
]
values2 = [
    [65,60,62],
    [57,50,53],
    [35,38,36],
    [38,40,37]
]
names = [
    "chuku","zhixian","zhuanwan","diaotou"
]

for i in range(2):
    for j in range(2):
        ax[i][j].plot( [1,2,3], values1[2*i+j], [1,2,3], values2[2*i+j])
        ax[i][j].set_title(names[2*i+j])
        ax[i][j].set_xlabel("times")
        ax[i][j].set_ylabel("cm")
        ax[i][j].set_ylim(10, 70)
        ax[i][j].xaxis.set_major_locator(x_major_locator)
        ax[i][j].grid(False)

fig.legend(['QA_20230505_daily','1.5.1.0410'],loc='upper right')
plt.show()

效果:
Figure_1.png

Reference:

https://www.runoob.com/matplotlib/matplotlib-grid.html
https://blog.csdn.net/gdengden/article/details/103847847/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于 matplotlib,你可以使用 `matplotlib.dates` 模块来处理日期和时间数据,并自定义 x 轴的时间间隔。下面是一种常见的方法: 首先,导入必要的库: ```python import matplotlib.pyplot as plt import matplotlib.dates as mdates ``` 然后,创建一个 figure 和 axes 对象: ```python fig, ax = plt.subplots() ``` 接下来,如果你有日期或时间数据,需要将其转换为 matplotlib 支持的格式。假设你有一个时间序列数组 `dates` 和一个对应的数据数组 `values`。 ```python # 将日期数据转换为 matplotlib 支持的格式 dates = matplotlib.dates.date2num(dates) ``` 然后,将数据绘制到图形上: ```python ax.plot(dates, values) ``` 最后,使用 `mdates` 模块中的 `AutoDateLocator` 和 `AutoDateFormatter` 来自动设置 x 轴的时间间隔和格式: ```python # 设置 x 轴的时间间隔和格式 ax.xaxis.set_major_locator(mdates.AutoDateLocator()) ax.xaxis.set_major_formatter(mdates.AutoDateFormatter()) ``` 完整的示例代码如下所示: ```python import matplotlib.pyplot as plt import matplotlib.dates as mdates # 创建 figure 和 axes 对象 fig, ax = plt.subplots() # 假设有日期和对应的数据数组 dates = [datetime.datetime(2022, 1, 1), datetime.datetime(2022, 1, 2), datetime.datetime(2022, 1, 3)] values = [10, 20, 30] # 将日期数据转换为 matplotlib 支持的格式 dates = matplotlib.dates.date2num(dates) # 绘制数据 ax.plot(dates, values) # 设置 x 轴的时间间隔和格式 ax.xaxis.set_major_locator(mdates.AutoDateLocator()) ax.xaxis.set_major_formatter(mdates.AutoDateFormatter()) plt.show() ``` 这样,你就可以根据需要自定义 x 轴的时间间隔
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锡城筱凯

你的鼓励是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值