Matplotlib x轴设置间隔

该文展示了如何使用matplotlib库在Python中创建折线图,并对图形进行定制,包括设置X轴刻度间隔为1以及移除网格线,以增强数据可视化的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用默认设置绘制折线图

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.youkuaiyun.com/gdengden/article/details/103847847/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

锡城筱凯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值