【问题标题】:Matplotlib Plot Points Over Time Where Old Points FadeMatplotlib 随着时间的推移绘制点,旧点逐渐消失
【发布时间】:2018-06-17 13:50:53
【问题描述】:

我想用 matplotlib 实现两个目标:

  • 动态更新散点图
  • 慢慢使之前迭代中绘制的点更加透明。

目前,我能够使用颜色图实现相反的目标。也就是说,我可以随着时间的推移绘制点,但最近的点看起来更透明。

是否可以使用 cmap 或其他工具在 matplotlib 上获得“褪色”效果?谢谢!我的代码如下:

def plotter_fader(iterations = 100, stay_open = True):
# Set up plot
fig, ax = plt.subplots()
x_data = []
y_data = []
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
t_vals = np.linspace(0,1, iterations)
cmap = (0.09803921568627451, 0.09803921568627451, 0.09803921568627451, .05)
for t in t_vals:
    # Get intermediate points
    intermediate = (1-t)*A + t*B
    new_xvals, new_yvals = ... #Get these through some process
    x_vals.extend(new_xvals)
    y_vals.extend(new_yvals)

    # Put new values in your plot
    plt.plot(x_vals, y_vals, '.', color = cmap)

    # Recompute plot limits
    ax.relim()

    # Set title and wait a little bit for 'smoothness'
    ax.set_xlabel('X Axis', size = 12)
    ax.set_ylabel('Y Axis', size = 12)
    ax.set_title('Time: %0.3f' %t)
    ax.autoscale_view()
    fig.canvas.draw()
    time.sleep(0.005)

# Stay open after plotting ends
while stay_open:
    pass

【问题讨论】:

  • 您是否查看过alpha,例如plt.plot(x, y, ColorShape, alpha=0.5)

标签: python python-3.x matplotlib


【解决方案1】:

就像通常的散点图一样,您可以定义一个值数组和一个将这些值映射到颜色的颜色图。您可以在每次迭代中更改此数组,以使旧点具有不同的值。

在下文中,我们将值 0 表示为透明,将值 1 表示为深蓝色,并使用这些颜色创建一个颜色图。
在每次迭代中,旧值乘以小于 1 的数字,新值设置为 1。

运行动画会因此产生淡化点。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
from matplotlib.colors import LinearSegmentedColormap

fig, ax = plt.subplots()
ax.set_xlabel('X Axis', size = 12)
ax.set_ylabel('Y Axis', size = 12)
ax.axis([0,1,0,1])
x_vals = []
y_vals = []
intensity = []
iterations = 100

t_vals = np.linspace(0,1, iterations)

colors = [[0,0,1,0],[0,0,1,0.5],[0,0.2,0.4,1]]
cmap = LinearSegmentedColormap.from_list("", colors)
scatter = ax.scatter(x_vals,y_vals, c=[], cmap=cmap, vmin=0,vmax=1)

def get_new_vals():
    n = np.random.randint(1,5)
    x = np.random.rand(n)
    y = np.random.rand(n)
    return list(x), list(y)

def update(t):
    global x_vals, y_vals, intensity
    # Get intermediate points
    new_xvals, new_yvals = get_new_vals()
    x_vals.extend(new_xvals)
    y_vals.extend(new_yvals)

    # Put new values in your plot
    scatter.set_offsets(np.c_[x_vals,y_vals])

    #calculate new color values
    intensity = np.concatenate((np.array(intensity)*0.96, np.ones(len(new_xvals))))
    scatter.set_array(intensity)

    # Set title
    ax.set_title('Time: %0.3f' %t)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals,interval=50)

plt.show()

【讨论】:

  • 这很好用!谢谢!!我想知道如何实现类似于 mpl 行的东西。把我的头撞在墙上,但我看不出有什么办法。
【解决方案2】:

这是你要找的吗?

from matplotlib import pyplot as plt 

# Creates new axis.
plt.axis([0, 10, 0, 1])

# Allows interactive plotting
plt.ion()

# Transparency
alpha = 1

# Plotting first point outside of loop because more convenient for example
point = plt.scatter(0.5, 0.5, alpha=alpha)
for i in range(10):
    # As the loop goes on, increase transparency, remove the current point, 
    # and plots another one, more transparent.
    alpha -= 0.1
    point.remove()
    point = plt.scatter(5, .5, alpha=alpha, color='r')
    plt.pause(0.05)

while True:
    plt.pause(0.05)

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多