【问题标题】:Matplotlib: common color map for 2 scatter plots within the same plotMatplotlib:同一图中 2 个散点图的通用颜色图
【发布时间】:2019-06-24 09:49:54
【问题描述】:

我在2D Numpy arrays 中存储了双变量时间序列。我想在同一个情节上绘制该系列的两个频道。每个系列都应该用一条根据频道着色的线来表示。在这些行之上,我想将系列的点绘制为点。这些应根据相同形状的第二个2D Numpy array 中的值进行着色。我的问题是:如何为两个通道共有的范围内的点设置颜色映射?

通过plt.plot()plt.scatter() 的双重调用,我设法为每个系列获得了不同颜色和点的线条,例如:

import matplotlib.pyplot as plt
import numpy as np

# Bivariate time-series of length 10
nchannel, length = 2, 10
array_series = np.random.random((nchannel, length))
array_colors = np.vstack([np.repeat(0, length), np.repeat(1, length)])

colormap = 'jet'
plt.plot(np.arange(length), array_series[0,:])
plt.scatter(np.arange(length), array_series[0,:], c=array_colors[0,:], cmap=colormap)
plt.plot(np.arange(length), array_series[1,:])
plt.scatter(np.arange(length), array_series[1,:], c=array_colors[1,:], cmap=colormap)

这会产生:

这不是想要的输出,因为所有的点都是深蓝色的,所以 array_colors 中的 0 和 1 之间的区别就消失了。我寻找类似用plt.scatter(..., c=array_colors, cmap=colormap) 替换plt.scatter(..., c=array_colors[i,:], cmap=colormap) 的东西。但是,后者会引发错误。欢迎任何解决此问题的想法!

【问题讨论】:

  • 你能显示预期的输出吗?或者你的 sn-p 有什么问题?
  • @majpark 运行你的代码后,我得到随机颜色点
  • @ZarakiKenpachi 我修改了 array_colors 定义以显示预期的输出。

标签: python matplotlib plot colors


【解决方案1】:

我猜你可以只使用数组的平面版本:

import matplotlib.pyplot as plt
import numpy as np

# Bivariate time-series of length 10
nchannel, length = 2, 10
array_series = np.random.random((nchannel, length))
array_colors = np.random.random((nchannel, length))

x = np.arange(length)

plt.plot(x, array_series[0,:])
plt.plot(x, array_series[1,:])

xs = np.tile(x, nchannel)
plt.scatter(xs, array_series.flat, c=array_colors.flat)

plt.show()

【讨论】:

    【解决方案2】:

    您可以使用参数vminvmax

    作为vmin 传递全局最小值,作为vmax 传递全局最大值。这将导致对scatter 的所有调用都缩放相同范围内的值,从而产生统一的色标。

    例子:

    import matplotlib.pyplot as plt
    import numpy as np
    
    nchannel, length = 2, 10
    array_series = np.random.random((nchannel, length))
    array_colors = np.vstack([np.repeat(0, length), np.repeat(1, length)])
    
    colormap = 'jet'
    vmin = np.min(array_colors)
    vmax = np.max(array_colors)
    x = np.arange(length)
    
    plt.plot(x, array_series[0,:])
    plt.scatter(x, array_series[0,:], vmin=vmin, vmax=vmax, c=array_colors[0,:], cmap=colormap)
    plt.plot(x, array_series[1,:])
    plt.scatter(x, array_series[1,:], vmin=vmin, vmax=vmax, c=array_colors[1,:], cmap=colormap)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-10
      • 2011-08-29
      • 2018-07-30
      • 1970-01-01
      • 2021-07-28
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多