【问题标题】:How can I do subplots in matplotlib with differents xlimit and size of axis-x?如何在 matplotlib 中使用不同的轴 x 限制和大小绘制子图?
【发布时间】:2020-05-10 07:23:41
【问题描述】:

我该如何解决这个问题?我想用 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)

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

【问题讨论】:

  • 请编辑您的问题并复制粘贴前面不带“>”的代码。在代码前后使用三个反引号```。

标签: python matplotlib size subplot


【解决方案1】:

你需要指定matplotlib.pyplot.subplots要创建的地块数量,

nrows = 2
ncols = 2
fig, ax = plt.subplots(nrows, ncols, sharey='row')

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

绘制到个人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(...)

【讨论】:

  • 我试过这个,但结果是一样的,一个大情节。我用 plt.subplot(421) 指定了行和列,没错,不是吗?或者,我不知道我是否正确放置了变量,感谢您的回答和支持。
  • @ThéréHernandez plt.subplot 创建单个图。我的答案是使用plt.subplots - 它创建了多个图。您可以使用plt.subplot,但您需要为每个情节调用一次
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2018-10-22
  • 2018-01-12
  • 2019-04-08
  • 2020-07-08
  • 2020-05-06
  • 1970-01-01
相关资源
最近更新 更多