具有用户定义的颜色范围和静态颜色图的二维颜色编码散点图
2D Color coded scatter plot with user defined color range and static colormap
我有 3 个向量 - x
、y
、vel
每个向量都有大约 8k 个值。我还有很多包含这 3 个向量的文件。所有文件都有不同的 x、y、vel。我想得到具有以下条件的多个散点图:
- 根据第三个变量进行颜色编码,即 vel.
- 一旦为颜色设置了范围(对于第一个文件中的数据),所有剩余文件的颜色范围应该保持不变。我不想要动态变化(颜色代码随着每个新文件而变化)。
- 想要绘制颜色条。
非常感谢你的想法!!
我附上了单个文件的代码。
import numpy as np
import matplotlib.pyplot as plt
# Create Map
cm = plt.cm.get_cmap('RdYlBu')
x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
vel = [cm(float(i)/(8000)) for i in xrange(8000)] # 8000 is the no. of values in each of x,y,vel vectors.
# 2D Plot
plt.scatter(x, y, s=27, c=vel, marker='o')
plt.axis('equal')
plt.savefig('testfig.png', dpi=300)
plt.show()
quit()
您将必须遍历所有数据文件以获得 vel
的最大值,我添加了几行代码(需要根据您的情况进行调整)来实现这一点.
因此,您的 colorbar
行已更改为使用 max_vel
,允许您使用固定值 8000
摆脱该代码。
此外,我冒昧地去除了点周围的黑色边缘,因为我发现它们 'obfuscate' 点的颜色。
最后,我添加了调整你的绘图代码以使用 axis
对象,它需要有一个颜色条。
import numpy as np
import matplotlib.pyplot as plt
# This is needed to iterate over your data files
import glob
# Loop over all your data files to get the maximum value for 'vel'.
# You will have to adjust this for your code
"""max_vel = 0
for i in glob.glob(<your files>,'r') as fr:
# Iterate over all lines
if <vel value> > max_vel:
max_vel = <vel_value>"""
# Create Map
cm = plt.cm.get_cmap('RdYlBu')
x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
# Plot the data
fig=plt.figure()
fig.patch.set_facecolor('white')
# Here we switch to an axis object
# Additionally, you can plot several of your files in the same figure using
# the subplot option.
ax=fig.add_subplot(111)
s = ax.scatter(x,y,c=vel,edgecolor=''))
# Here we assign the color bar to the axis object
cb = plt.colorbar(mappable=s,ax=ax,cmap=cm)
# Here we set the range of the color bar based on the maximum observed value
# NOTE: This line only changes the calculated color and not the display
# 'range' of the legend next to the plot, for that we need to switch to
# ColorbarBase (see second code snippet).
cb.setlim(0,max_vel)
cb.set_label('Value of \'vel\'')
plt.show()
片段,演示 ColorbarBase
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
cm = plt.cm.get_cmap('RdYlBu')
x = [1,5,10]
y = [2,6,9]
vel = [7,2,1]
# Plot the data
fig=plt.figure()
fig.patch.set_facecolor('white')
ax=fig.add_subplot(111)
s = ax.scatter(x,y,c=vel,edgecolor=''))
norm = mpl.colors.Normalize(vmin=0, vmax=10)
ax1 = fig.add_axes([0.95, 0.1, 0.01, 0.8])
cb = mpl.colorbar.ColorbarBase(ax1,norm=norm,cmap=cm,orientation='vertical')
cb.set_clim(vmin = 0, vmax = 10)
cb.set_label('Value of \'vel\'')
plt.show()
这会产生以下情节
有关 colorbar
的更多示例,特别是更灵活的 ColorbarBase
,我建议您查看文档 -> http://matplotlib.org/examples/api/colorbar_only.html
我有 3 个向量 - x
、y
、vel
每个向量都有大约 8k 个值。我还有很多包含这 3 个向量的文件。所有文件都有不同的 x、y、vel。我想得到具有以下条件的多个散点图:
- 根据第三个变量进行颜色编码,即 vel.
- 一旦为颜色设置了范围(对于第一个文件中的数据),所有剩余文件的颜色范围应该保持不变。我不想要动态变化(颜色代码随着每个新文件而变化)。
- 想要绘制颜色条。
非常感谢你的想法!!
我附上了单个文件的代码。
import numpy as np
import matplotlib.pyplot as plt
# Create Map
cm = plt.cm.get_cmap('RdYlBu')
x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
vel = [cm(float(i)/(8000)) for i in xrange(8000)] # 8000 is the no. of values in each of x,y,vel vectors.
# 2D Plot
plt.scatter(x, y, s=27, c=vel, marker='o')
plt.axis('equal')
plt.savefig('testfig.png', dpi=300)
plt.show()
quit()
您将必须遍历所有数据文件以获得 vel
的最大值,我添加了几行代码(需要根据您的情况进行调整)来实现这一点.
因此,您的 colorbar
行已更改为使用 max_vel
,允许您使用固定值 8000
摆脱该代码。
此外,我冒昧地去除了点周围的黑色边缘,因为我发现它们 'obfuscate' 点的颜色。
最后,我添加了调整你的绘图代码以使用 axis
对象,它需要有一个颜色条。
import numpy as np
import matplotlib.pyplot as plt
# This is needed to iterate over your data files
import glob
# Loop over all your data files to get the maximum value for 'vel'.
# You will have to adjust this for your code
"""max_vel = 0
for i in glob.glob(<your files>,'r') as fr:
# Iterate over all lines
if <vel value> > max_vel:
max_vel = <vel_value>"""
# Create Map
cm = plt.cm.get_cmap('RdYlBu')
x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
# Plot the data
fig=plt.figure()
fig.patch.set_facecolor('white')
# Here we switch to an axis object
# Additionally, you can plot several of your files in the same figure using
# the subplot option.
ax=fig.add_subplot(111)
s = ax.scatter(x,y,c=vel,edgecolor=''))
# Here we assign the color bar to the axis object
cb = plt.colorbar(mappable=s,ax=ax,cmap=cm)
# Here we set the range of the color bar based on the maximum observed value
# NOTE: This line only changes the calculated color and not the display
# 'range' of the legend next to the plot, for that we need to switch to
# ColorbarBase (see second code snippet).
cb.setlim(0,max_vel)
cb.set_label('Value of \'vel\'')
plt.show()
片段,演示 ColorbarBase
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
cm = plt.cm.get_cmap('RdYlBu')
x = [1,5,10]
y = [2,6,9]
vel = [7,2,1]
# Plot the data
fig=plt.figure()
fig.patch.set_facecolor('white')
ax=fig.add_subplot(111)
s = ax.scatter(x,y,c=vel,edgecolor=''))
norm = mpl.colors.Normalize(vmin=0, vmax=10)
ax1 = fig.add_axes([0.95, 0.1, 0.01, 0.8])
cb = mpl.colorbar.ColorbarBase(ax1,norm=norm,cmap=cm,orientation='vertical')
cb.set_clim(vmin = 0, vmax = 10)
cb.set_label('Value of \'vel\'')
plt.show()
这会产生以下情节
有关 colorbar
的更多示例,特别是更灵活的 ColorbarBase
,我建议您查看文档 -> http://matplotlib.org/examples/api/colorbar_only.html