numpy配列に格納されたポイントxとyの範囲があります。それらはx(t)とy(t)を表し、t = 0 ... T-1
私はを使用して散布図をプロットしています
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
時間を表すカラーマップが必要です(したがって、numpy配列のインデックスに応じてポイントに色を付けます)
そうする最も簡単な方法は何ですか?
ここに例があります
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
t = np.arange(100)
plt.scatter(x, y, c=t)
plt.show()
ここt
では、の配列であるインデックスに基づいて色を設定しています[1, 2, ..., 100]
。
おそらく、理解しやすい例は、やや単純なものです。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
plt.scatter(x, y, c=t)
plt.show()
Note that the array you pass as c
doesn't need to have any particular order or type, i.e. it doesn't need to be sorted or integers as in these examples. The plotting routine will scale the colormap such that the minimum/maximum values in c
correspond to the bottom/top of the colormap.
You can change the colormap by adding
import matplotlib.cm as cm
plt.scatter(x, y, c=t, cmap=cm.cmap_name)
Importing matplotlib.cm
is optional as you can call colormaps as cmap="cmap_name"
just as well. There is a reference page of colormaps showing what each looks like. Also know that you can reverse a colormap by simply calling it as cmap_name_r
. So either
plt.scatter(x, y, c=t, cmap=cm.cmap_name_r)
# or
plt.scatter(x, y, c=t, cmap="cmap_name_r")
will work. Examples are "jet_r"
or cm.plasma_r
. Here's an example with the new 1.5 colormap viridis:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='viridis')
ax2.scatter(x, y, c=t, cmap='viridis_r')
plt.show()
You can add a colorbar by using
plt.scatter(x, y, c=t, cmap='viridis')
plt.colorbar()
plt.show()
数値とサブプロットを明示的に使用している場合(fig, ax = plt.subplots()
またはなどax = fig.add_subplot(111)
)、カラーバーの追加は少し複雑になる可能性があることに注意してください。ここでは、単一のサブプロットカラーバーと2つのサブプロットと1つのカラーバーの良い例を見つけることができます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加