如何在matplotlib中更新条形图?

2024-05-15 00:42:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有条形图,有很多自定义特性(label、linewidth、edgecolor)

import matplotlib.pyplot as plt
fig = plt.figure()
ax  = plt.gca()

x = np.arange(5)
y = np.random.rand(5)

bars = ax.bar(x, y, color='grey', linewidth=4.0)

ax.cla()
x2 = np.arange(10)
y2 = np.random.rand(10)
ax.bar(x2,y2)
plt.show() 

对于'normal'图,我会使用set_data(),但是对于barchart,我得到了一个错误:AttributeError: 'BarContainer' object has no attribute 'set_data'

我不想简单地更新矩形的高度,我想绘制全新的矩形。如果我使用ax.cla(),我的所有设置(linewidth、edgecolor、title..)都将丢失,不仅是我的数据(矩形),而且要清除多次,重置所有设置都会使我的程序滞后。如果我不使用ax.cla(),设置保持不变,程序会更快(我不必一直设置属性),但矩形是相互绘制的,这是不好的。

你能帮我吗?


Tags: datanpbarpltrandomaxx2矩形

热门问题