我正在从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()
当前,脚本输出类似于以下内容。
如您所见,最后一个绘图已调整了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()
这是结果的图像。