Rak*_*him 6 python matplotlib scatter-plot colorbar
我必须在绘图上显示的信息是2个坐标:大小和颜色(无填充).颜色很重要,因为我需要一种颜色图表类型的图表来根据颜色值显示信息.
我尝试了两种不同的方法:
创建特定圈子并添加各个圈子.
circle1 = plt.Circle(x, y, size, color='black', fill=False)
ax.add_artist(circle1)
Run Code Online (Sandbox Code Playgroud)这种方法的问题是我找不到根据颜色值设置颜色的方法.即,对于0-1的值范围,我希望0为完全蓝色而1为完全红色,因此在它们之间是紫色的不同色调,其红色/蓝色取决于颜色值的高/低.
之后我尝试使用分散功能:
size.append(float(Info[i][8]))
plt.scatter(x, y, c=color, cmap='jet', s=size, facecolors='none')
Run Code Online (Sandbox Code Playgroud)这种方法的问题是大小似乎没有变化,这可能是我创建数组大小的方式的原因.因此,如果我用大数字替换大小,则图表显示为圆圈.这facecolours = 'none'只是为了绘制周长.
我相信这两种方法都可以实现您的目标.首先绘制未填充的圆圈,然后使用相同的点绘制散点图.对于散点图,请将大小设置为0,但使用它来设置颜色条.
请考虑以下示例:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
# generate some random data
npoints = 5
x = np.random.randn(npoints)
y = np.random.randn(npoints)
# make the size proportional to the distance from the origin
s = [0.1*np.linalg.norm([a, b]) for a, b in zip(x, y)]
s = [a / max(s) for a in s] # scale
# set color based on size
c = s
colors = [cm.jet(color) for color in c] # gets the RGBA values from a float
# create a new figure
plt.figure()
ax = plt.gca()
for a, b, color, size in zip(x, y, colors, s):
# plot circles using the RGBA colors
circle = plt.Circle((a, b), size, color=color, fill=False)
ax.add_artist(circle)
# you may need to adjust the lims based on your data
minxy = 1.5*min(min(x), min(y))
maxxy = 1.5*max(max(x), max(y))
plt.xlim([minxy, maxxy])
plt.ylim([minxy, maxxy])
ax.set_aspect(1.0) # make aspect ratio square
# plot the scatter plot
plt.scatter(x,y,s=0, c=c, cmap='jet', facecolors='none')
plt.grid()
plt.colorbar() # this works because of the scatter
plt.show()
Run Code Online (Sandbox Code Playgroud)
我的一次跑步的示例情节:
| 归档时间: |
|
| 查看次数: |
10776 次 |
| 最近记录: |