python - 在 Python 中使用 matplotlib 在 4x4 多重图上显示 X 和 Y 刻度

标签 python python-3.x matplotlib plot axis-labels

我正在使用 Anaconda 安装中的 Python3.6.5。

我有 16 个数据文件,其中包含两列数据。我正在尝试制作一个图,在一个 4x4 图中显示所有数据。我已经设法将所有绘图绘制在大型 4x4 绘图上,但无法调整 X 和 Y 刻度。 X 值的范围是 0 到 2000,Y 值的范围是 0 到 4.5。

这是我当前的脚本:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import math

ph_values = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5]

all_xs = []
all_ys = []
for ph in ph_values:
    xs = []
    ys = []
    with open('rmsd_ph' + str(ph) + '.dat', "r") as f:
        for line in f:
            if line[0] != "#":
               x,y = line.split()
               xs.append(float(x))
               ys.append(float(y))
    all_xs.append(xs)
    all_ys.append(ys)

fig, axes = plt.subplots(nrows=math.ceil(len(ph_values)/4), ncols=4, figsize=(6,6))

axes = axes.flatten()
for index,ph in enumerate(ph_values):
    axes[index].plot(np.asarray(all_xs[index]),np.asarray(all_ys[index]))

plt.xticks(np.arange(0, 2000, step=500))
plt.tight_layout()
plt.savefig('test.pdf')
plt.show()

当前脚本输出的内容如下所示。

enter image description here

如您所见,最后一张图已调整 X 轴。我还没有尝试调整 Y 轴,因为我还没有成功调整 y 轴。

总的来说,我希望 y 轴和 x 轴上都有 4 个刻度。

最佳答案

这就是我发现的解决我遇到的问题的方法。

fig, axes = plt.subplots(nrows=math.ceil(len(ph_values)/4), ncols=4, figsize=(9,9))

axes = axes.flatten()
for index,ph in enumerate(ph_values):
    axes[index].scatter(np.asarray(all_xs[index]),np.asarray(all_ys[index]), s=1)
    plt.sca(axes[index])  <------------------  Fixed Problem 
    plt.xticks([0, 500, 1000, 1500, 2000]) <-  Fixed Problem
    plt.yticks([0, 1, 2, 3, 4, 5]) <---------- Fixed Problem
    plt.title('pH:' + str(ph)) 
    if (index % 4 == 0):
        plt.ylabel('RMSD [$\\rm{\\AA}$]')
    if (index >= 12):
        plt.xlabel('Steps')


plt.tight_layout()
plt.savefig(output)
plt.show()

这是结果的图像。

Final Image

关于python - 在 Python 中使用 matplotlib 在 4x4 多重图上显示 X 和 Y 刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50846666/

相关文章:

python - 仅用于表情符号的词云

Python 破折号 : Fitting a table and a graph in one row of a grid

Python tkinter,而不是事件错误

python - 独立于数据集单位的 Matplotlib 轴限制和文本位置

python - 如何向 subplot2grid 添加颜色条

python - 如何摆脱浮零(-0.0)中的负号?

python - 将 dict 保存为 JSON 以便它们是人类可读的

python - 如何用 df.loc 或 df.iloc 替换 df.ix?

python - 我怎样才能清理这个请求结果?

python mpl : how to plot a point on a graph of 3D vectors, 和箭头头