我对Matplotlib来说是全新的,我已经编写了这段代码来绘制到目前为止工作正常的两个系列:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
gs = gridspec.GridSpec(3,1)
if not somecondition:
ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
else:
ax = plt.subplot(gs[:2, :])
ax.plot(list1)
ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there
ax.plot(list2) #populate the second subplot
plt.show()
我想要做的是在这个情节中添加第三个系列,让我们说:
list3 = [4,1,2,4]
重要的是第一个子图(list1)必须比其他两个大两倍;为了做到这一点,我使用了gridspace,但由于我是新手,我无法理解如何设置此示例代码的参数以获得第三个。任何人都可以解释我应该如何编辑块somecondition == True
以获得3个子图(前1个比下面的2个大两倍)而不仅仅是两个?
附:代码是可执行的。
答案 0 :(得分:1)
这是Matplotlib子图
的示例import matplotlib.pyplot as plt
import numpy as np
x,y = np.random.randn(2,100)
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2)
ax1.grid(True)
ax1.axhline(0, color='black', lw=2)
ax2 = fig.add_subplot(212, sharex=ax1)
ax2.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2)
ax2.grid(True)
ax2.axhline(0, color='black', lw=2)
plt.show()
它使用pyplot
和add_subplot
语法相当简单。
答案 1 :(得分:0)
要获得2:1的比例,您可以使用4行,并且分别绘制2,1,1行:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
list1 = [1,2,3,4]
list2 = [4,3,2,1]
list3 = [4,1,2,4]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
gs = gridspec.GridSpec(4,1)
if not somecondition:
ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
else:
ax = plt.subplot(gs[:2, :])
ax.plot(list1)
ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there
ax.plot(list2) #populate the second subplot
ax = plt.subplot(gs[3, :]) #create the second subplot, that MIGHT be there
ax.plot(list3)
plt.show()