Matplotlib x-axis setting interval

Draw a line chart with default settings

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()

Effect:
Figure_1.png

Line chart after setting the interval

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()

Effect:
Figure_1.png

The effect after removing the grid lines

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()

Effect:
Figure_1.png

Reference:

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

Guess you like

Origin blog.csdn.net/xiaokai1999/article/details/130505819