如何在Matplotlib Python中动态更改Alpha值 [英] How can I change the alpha value dynamically in matplotlib python

查看:88
本文介绍了如何在Matplotlib Python中动态更改Alpha值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何动态更改已绘制的 alpha 值.

I'm seeking how to change an alpha value dynamically which are already plotted.

这是我要实现的一种示例代码,但我知道这是错误的写作.

This is a kind of sample code I want to implement, but I know it is a wrong writing.

import matplotlib.pyplot as plt

fig = plt.subplot(1, 1)

for rate in [0.1 * x for x in range(10, -1, -1)]:
    plt.plot(range(0, 5), range(0, 5), color="r", alpha=rate)
    plt.pause(0.1)

plt.show()

此示例代码的目的是我想在处理进行时减小alpha并使线消失.

The purpose of this sample code is that I want to decrease the alpha as the processing go on and make the line vanish.

有人知道解决这个问题的方法吗?谢谢.

Does somebody know a way to accomplish like this problem? Thank you.

推荐答案

您可以使用 set_alpha 方法更新现有 Line2D 的 alpha 值.这个想法是绘制一次线,然后在循环中更新 alpha.

You can update the alpha value of an existing Line2D, using the set_alpha method. The idea would be to plot the line once and then update the alpha in the loop.

import matplotlib.pyplot as plt

fig = plt.subplot(111)
plt.ion()
line, = plt.plot(range(0, 5), range(0, 5), color="r", alpha=1)
for rate in [0.1 * x for x in range(10, -1, -1)]:
    line.set_alpha(rate)
    plt.draw()
    plt.pause(0.1)

plt.ioff()
plt.show()

这篇关于如何在Matplotlib Python中动态更改Alpha值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆