使用 numpy 和 pandas 绘制小刻度

问题描述 投票:0回答:1

如何向使用 numpy 完成的绘图添加小刻度? 我有一个包含两列 A 和 B 的文件,我需要使用 numpy 将每一列乘以不同的因子,并且我想用较小的刻度来绘制它们(有几个文件,我想将它们全部绘制在子图中),但是numpy 没有小刻度的属性。


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
fig, plt= plt.subplots(7,1, sharex=True,figsize=(5,12))
file = pd.read_csv('file1.txt',header=0,delimiter= '\s+', index_col=False)
plt[0].plot(file.A.multiply(33.4),file.B.multiply(1/144))

我试过了:

plt.setx.set_minor_locator(MultipleLocator(5))

每 5 个值绘制次要刻度。

python pandas numpy matplotlib
1个回答
0
投票

您可以直接访问图中每个子图中的 x 轴并应用 set_minor_locator 函数。

import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.ticker import MultipleLocator
import numpy as np
fig, axs = plt.subplots(7, 1, sharex=True, figsize=(5, 12))

for i in range(7):
    file = pd.read_csv(f'file{i+1}.txt', header=0, delimiter='\s+', index_col=False)
    axs[i].plot(file.A.multiply(33.4), file.B.multiply(1/144))
    axs[i].xaxis.set_minor_locator(MultipleLocator(5))

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.