from matplotlib import pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [125, 32, 54, 253, 67, 87, 233, 56, 67]
color = [str(item/255.) for item in y]
plt.scatter(x, y, s=500, c=color)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Generate data...
x = np.random.random(10)
y = np.random.random(10)
# Plot...
plt.scatter(x, y, c=y, s=500)
plt.gray()
plt.show()
或者,如果您喜欢wider range of colormaps,也可以指定cmapkwarg到scatter。要使用其中任何一个的反向版本,只需指定其中任何一个的“_r”版本。E、 g.gray_r而不是gray。有几种不同的灰度彩色地图是预先制作的(例如,gray,gist_yarg,binary,等等)。
import matplotlib.pyplot as plt
import numpy as np
# Generate data...
x = np.random.random(10)
y = np.random.random(10)
plt.scatter(x, y, c=y, s=500, cmap='gray')
plt.show()
x=['A','B','B','C','A','B']
y=[15,30,25,18,22,13]
# Function to map the colors as a list from the input list of x variables
def pltcolor(lst):
cols=[]
for l in lst:
if l=='A':
cols.append('red')
elif l=='B':
cols.append('blue')
else:
cols.append('green')
return cols
# Create the colors list using the function above
cols=pltcolor(x)
plt.scatter(x=x,y=y,s=500,c=cols) #Pass on the list created by the function here
plt.grid(True)
plt.show()
在matplotlib中,灰色可以作为介于0-1之间的数值字符串给出。
例如
c = '0.1'
然后,您可以将第三个变量转换为该范围内的值,并使用该值为点上色。
在下面的示例中,我使用点的y位置作为确定颜色的值:
不需要手动设置颜色。相反,请指定灰度颜色映射。。。
或者,如果您喜欢wider range of colormaps,也可以指定
cmap
kwarg到scatter
。要使用其中任何一个的反向版本,只需指定其中任何一个的“_r
”版本。E、 g.gray_r
而不是gray
。有几种不同的灰度彩色地图是预先制作的(例如,gray
,gist_yarg
,binary
,等等)。有时可能需要根据x值大小写精确绘制颜色。例如,您可能有一个包含3种类型变量和一些数据点的数据帧。你想做的是
在这种情况下,您可能需要编写to short函数,将x值映射为相应的颜色名称作为列表,然后将该列表传递给
plt.scatter
命令。相关问题 更多 >
编程相关推荐