如何删除AxesSubplot对象上的额外图?

问题描述 投票:0回答:1

我有一个AxesSubplot对象ax1

fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)

我在这个ax1上多次绘制,以了解alpha值将如何设置绘图的外观:

first = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
second = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.6)
third = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.9)

但这三个图相互重叠:

如何删除以前的直方图,然后每次只显示一个图?顺便说一句,alpha arg做了什么?

谢谢。 :)

python matplotlib
1个回答
1
投票

如果我明白你想要什么,那就试试吧

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)

输出enter image description here

first = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
second = ax2.hist(np.random.randn(100), bins=20, color='k', alpha=0.6)
third = ax3.hist(np.random.randn(100), bins=20, color='k', alpha=0.9)

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.