簡體   English   中英

Matplotlib:同一圖中兩個散點圖的通用顏色圖

[英]Matplotlib: common color map for 2 scatter plots within the same plot

我將雙變量時間序列存儲在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[i,:], cmap=colormap)替換plt.scatter(..., c=array_colors[i,:], cmap=colormap) plt.scatter(..., c=array_colors, cmap=colormap) 但是,后者會引發錯誤。 任何解決這個問題的想法都將受到歡迎!

我猜你只能使用數組的平面版本:

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()

您可以使用參數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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM