如何在 matplotlib 中使用不同的限制和大小 axis-x 来绘制子图?

How can I do subplots in matplotlib with differents xlimit and size of axis-x?

我该如何解决这个问题?我想用 matplotlib 做 4 个子图,我使用了 subplot 选项,但结果只是一个大图。我不知道是什么问题。我想看四个子图,每个子图都有标题,还有一个副标题。

我不知道该如何解决?

你能帮我解决一下吗? 非常感谢

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from matplotlib.collections import LineCollection
import matplotlib.patches as mpatches
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.ticker as tkr
from pylab import text


with open("file1.txt") as f:
         m1 = map(float,f)

with open ("file2.txt") as f:
         m2 = map(float, f)

fig, ax = plt.subplots(sharey='row')
fig.set_figwidth(18)  #Width figure
fig.set_figheight(12) #Height figure

plt.rcParams['figure.dpi'] = 300
plt.subplots_adjust(wspace=0.18, hspace=0.2)

fig.suptitle('PLOTS', y=0.93, fontsize=15)

# Plot
plt.subplot(421)
y = np.array(m1)
x = np.arange(len(y))
threshold = 0.5
segments_x = np.r_[x[0], x[1:-1].repeat(2), x[-1]].reshape(-1, 2)
segments_y = np.r_[y[0], y[1:-1].repeat(2), y[-1]].reshape(-1, 2)
linecolors = ['red' if y_[0] > threshold and y_[1] > threshold else 'blue'
              for y_ in segments_y]
segments = [zip(x_, y_) for x_, y_ in zip(segments_x, segments_y)]
ax = plt.axes()
ax.add_collection(LineCollection(segments, colors=linecolors))
ax.set_ylim(-0.06, 1.07)
ax.set_xlim(0,268)
blue_patch = mpatches.Patch(color='blue', label='ordenada')
red_patch = mpatches.Patch(color='red', label='desordenada')
plt.legend(handles=[blue_patch, red_patch], loc='lower left', fontsize=12)
plt.axhline(y=0.5, color='black', linestyle='--')
plt.title(r'Protein', fontsize=18)
plt.xlabel(r'# Residue', fontsize=16)
plt.ylabel(r'(%)', fontsize=16)
plt.xticks(size=12)
plt.yticks(size=12)
plt.xticks(np.arange(min(x), max(x)+1, 10))
plt.grid()
plt.tight_layout()

# Plot
plt.subplot(423)
p = np.array(m2)
o = np.arange(len(p))
threshold = 0.5
segments_o = np.r_[o[0], o[1:-1].repeat(2), o[-1]].reshape(-1, 2)
segments_p = np.r_[p[0], p[1:-1].repeat(2), p[-1]].reshape(-1, 2)
linecolors = ['red' if p_[0] > threshold and p_[1] > threshold else 'blue'
              for p_ in segments_p]
segments = [zip(o_, p_) for o_, p_ in zip(segments_o, segments_p)]
ax = plt.axes()
ax.add_collection(LineCollection(segments, colors=linecolors))
ax.set_ylim(-0.06, 1.07)
ax.set_xlim(0,383)
blue_patch = mpatches.Patch(color='blue', label='ordenada')
red_patch = mpatches.Patch(color='red', label='desordenada')
plt.legend(handles=[blue_patch, red_patch], loc='lower left', fontsize=12)
plt.axhline(y=0.5, color='black', linestyle='--')
plt.title(r'Protein', fontsize=18)
plt.xlabel(r'# Residue', fontsize=16)
plt.ylabel(r'(%)', fontsize=16)
plt.xticks(size=12)
plt.yticks(size=12)
plt.xticks(np.arange(min(o), max(o)+1, 10))
plt.grid()
plt.tight_layout()
plt.show()

#plt.savefig('figure.png', format='png', bbox_inches="tight", dpi=300)

我该如何解决这个问题? 问题出在哪里?

您需要指定要通过 matplotlib.pyplot.subplots

创建的地块数量
nrows = 2
ncols = 2
fig, ax = plt.subplots(nrows, ncols, sharey='row')

这将创建一个 axes 个实例数组,形状为 (nrows, ncols)。然后,您可以通过

绘制到个人 axes
ax[0,0].plot(...)

尽管要为 axes 设置刻度属性、标签等,您需要使用函数的 axes 版本而不是 pyplot 版本。即

ax[0, 0].set_xticks(...)
# instead of 
plt.xticks(...)

ax[0, 0].set_title(...)
# instead of 
plt.title(...)

ax[0, 0].set_xlabel(...)
# instead of
plt.set_xlabel(...)