下面生成两个绘图,一个在Jupyter笔记本上的另一个上。如何使函数plotAllDist(…)从plot1Dist(…)获取绘图,并将其作为子绘图并排打印
我试着读了一些帖子,但是没有用
def plot1Dist(x, sigmaJ, pmf, title):
fig = plt.figure()
freqTable = np.array(np.unique(sigmaJ, return_counts=True)).T
simu = plt.plot(freqTable[:,0], freqTable[:,1], label='Simulation')
dist = pmf * sum(freqTable[:,1])
model = plt.plot(x, dist, label='Model')
# add description to the plot
plt.legend(loc="upper right")
plt.xlabel('sigma')
plt.ylabel('Frequency')
plt.xticks(np.arange(min(x), max(x)+1, 5))
plt.title(title)
plt.show()
def plotAllDist(x, sigmaJ, e, pmf0, pmf1, FLIPS):
ONES = [i for i in range(e.size) if e[i] == 1]
ZEROS = [j for j in range(e.size) if e[j] == 0]
if (FLIPS == 0):
title = 'Distribution of sigma before bit-flipping'
elif (FLIPS > 0):
title = 'Distribution of sigma after ' + str(FLIPS) + ' flips'
plot1Dist(x, sigmaJ[ZEROS], pmf0, title + ' (e=0)')
plot1Dist(x, sigmaJ[ONES], pmf1, title + ' (e=1)')
您应该使用^{} 和object-oriented API for matplotlib。首先,避免在
plot1Dist
内创建新图形,因为这样每次都会强制创建一个新的绘图(因此,在笔记本中,在屏幕上一个接一个地绘制)我将修改您的代码如下:
我真的鼓励你去看看object-oriented API!在我看来,它比“pyplot”API更容易混淆,也更灵活
相关问题 更多 >
编程相关推荐