如何让 matplotlib 的“子图”中的每个图使用不同的轴?

Eiy*_*uyf 5 axes matplotlib

因此,当我尝试使用绘制多个子图时,pyplot.subplots我会得到类似的信息:

四个子图

我怎么能有:

  1. 每个子图的多个独立轴
  2. 每个子图的轴
  3. 使用子图在每个子图轴上叠加图。我试着做((ax1,ax2),(ax3,ax4)) = subplots然后做ax1.plot两次,但结果,两者都没有显示。

图片代码:

import string
import matplotlib
matplotlib.use('WX')

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
from itertools import izip,chain


f,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2,sharex='col',sharey='row')

ax1.plot(range(10),2*np.arange(10))
ax2.plot(range(10),range(10))
ax3.plot(range(5),np.arange(5)*1000)
#pyplot.yscale('log')
#ax2.set_autoscaley_on(False)
#ax2.set_ylim([0,10])


plt.show()
Run Code Online (Sandbox Code Playgroud)

Dre*_*rew 7

问题 1 和 2:

为此,请明确设置子图选项sharexsharey=False

替换代码中的这一行以获得所需的结果。

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=False, sharey=False)
Run Code Online (Sandbox Code Playgroud)

或者,这两个选项可以完全省略,这False是默认值。(如下rubenvb所述)

问题 3:

以下是将辅助图添加到两个子图的两个示例:

(在此之前添加此代码段plt.show()

# add an additional line to the lower left subplot
ax3.plot(range(5), -1*np.arange(5)*1000)

# add a bar chart to the upper right subplot
width = 0.75       # the width of the bars
x = np.arange(2, 10, 2)
y = [3, 7, 2, 9]

rects1 = ax2.bar(x, y, width, color='r')
Run Code Online (Sandbox Code Playgroud)

具有独立轴的子图和“多个”图


tac*_*ell 0

不要告诉它共享轴:

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

ax1.plot(range(10),2*np.arange(10))
ax2.plot(range(10),range(10))
ax3.plot(range(5),np.arange(5)*1000)
Run Code Online (Sandbox Code Playgroud)

文档