最近需要实现数据可视化,进而了解了一下Python作图的一些小技巧,在这里记录一下,以备不时之需~~
我们在绘制图像时,为了区分不同的点,往往会希望在图像上将不同的点用不同的颜色显示出来。
下面以matplotplib.pyplot中散点图:scatter()为例,介绍其表示颜色的参数c的方法:
import matplotlib.pyplot as plt
# 颜色可用元组表示,元组由4各元素组成,每个元素范围在[0,1]之间
# 比如(1,0.1,0.2,0.3)就是一种颜色;(0.8,0.8,0.1,1)是另外一种颜色
plt.scatter(x=[1], y=[1], c=(0.8, 0.8, 0.1, 1), marker='o', s=200)
plt.scatter(x=[1.5], y=[1.5], c=(0.6, 0.9, 0.5, 1), marker='*', s=200)
plt.scatter(x=[2], y=[2], c=(1, 0.5, 0.3, 1), marker='<', s=200)
plt.show()
import matplotlib.pyplot as plt
# 用cm.Set1, cm.Set2, cm.Set3返回颜色
color1 = plt.cm.Set1(1)
color2 = plt.cm.Set2(1)
color3 = plt.cm.Set3(1)
print(color1, '\n', color2, '\n', color3)
plt.scatter(x=[2, 3, 4], y=[2, 3, 4], c=[color1, color2, color3], marker='*', s=200)
这里的输出结果为:
(0.21568627450980393, 0.49411764705882355, 0.7215686274509804, 1.0)
(0.9882352941176471, 0.5529411764705883, 0.3843137254901961, 1.0)
(1.0, 1.0, 0.7019607843137254, 1.0)
import matplotlib.pyplot as plt
# 用cm.Set1返回不同颜色
for i in range(5, 8):
color = plt.cm.Set1(i)
print('color', i, color)
plt.scatter(i, i, c=color, marker='^', s=200)
plt.show()
这里输出的结果为:
color 5 (1.0, 1.0, 0.2, 1.0)
color 6 (0.6509803921568628, 0.33725490196078434, 0.1568627450980392, 1.0)
color 7 (0.9686274509803922, 0.5058823529411764, 0.7490196078431373, 1.0)
在这里用for循环来看一下不同的num1,num2有什么颜色的区别:
import matplotlib.pyplot as plt
# 用cm.Set1,cm.Set2,cm.Set3返回不同颜色
for i in range(0, 20):
color = plt.cm.Set1(i)
# color = plt.cm.Set2(i)
# color = plt.cm.Set3(i)
print('color', i, color)
plt.scatter(i, i, c=color, marker='^', s=200)
plt.show()
结果为:
Set1(num2)时,由运行结果可以看出,Set1中,num2的取值范围为[0, 8]。
color 0 (0.8941176470588236, 0.10196078431372549, 0.10980392156862745, 1.0)
color 1 (0.21568627450980393, 0.49411764705882355, 0.7215686274509804, 1.0)
color 2 (0.30196078431372547, 0.6862745098039216, 0.2901960784313726, 1.0)
color 3 (0.596078431372549, 0.3058823529411765, 0.6392156862745098, 1.0)
color 4 (1.0, 0.4980392156862745, 0.0, 1.0)
color 5 (1.0, 1.0, 0.2, 1.0)
color 6 (0.6509803921568628, 0.33725490196078434, 0.1568627450980392, 1.0)
color 7 (0.9686274509803922, 0.5058823529411764, 0.7490196078431373, 1.0)
color 8 (0.6, 0.6, 0.6, 1.0)
color 9 (0.6, 0.6, 0.6, 1.0)
color 10 (0.6, 0.6, 0.6, 1.0)
color 11 (0.6, 0.6, 0.6, 1.0)
color 12 (0.6, 0.6, 0.6, 1.0)
color 13 (0.6, 0.6, 0.6, 1.0)
color 14 (0.6, 0.6, 0.6, 1.0)
color 15 (0.6, 0.6, 0.6, 1.0)
color 16 (0.6, 0.6, 0.6, 1.0)
color 17 (0.6, 0.6, 0.6, 1.0)
color 18 (0.6, 0.6, 0.6, 1.0)
color 19 (0.6, 0.6, 0.6, 1.0)
Set2(num2)时,由运行结果可以看出,Set2中,num2的取值范围为[0, 7]。
color 0 (0.4, 0.7607843137254902, 0.6470588235294118, 1.0)
color 1 (0.9882352941176471, 0.5529411764705883, 0.3843137254901961, 1.0)
color 2 (0.5529411764705883, 0.6274509803921569, 0.796078431372549, 1.0)
color 3 (0.9058823529411765, 0.5411764705882353, 0.7647058823529411, 1.0)
color 4 (0.6509803921568628, 0.8470588235294118, 0.32941176470588235, 1.0)
color 5 (1.0, 0.8509803921568627, 0.1843137254901961, 1.0)
color 6 (0.8980392156862745, 0.7686274509803922, 0.5803921568627451, 1.0)
color 7 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 8 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 9 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 10 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 11 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 12 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 13 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 14 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 15 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 16 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 17 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 18 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
color 19 (0.7019607843137254, 0.7019607843137254, 0.7019607843137254, 1.0)
Set3(num2)时,由运行结果可以看出,Set3中,num2的取值范围为[0, 11]。
color 0 (0.5529411764705883, 0.8274509803921568, 0.7803921568627451, 1.0)
color 1 (1.0, 1.0, 0.7019607843137254, 1.0)
color 2 (0.7450980392156863, 0.7294117647058823, 0.8549019607843137, 1.0)
color 3 (0.984313725490196, 0.5019607843137255, 0.4470588235294118, 1.0)
color 4 (0.5019607843137255, 0.6941176470588235, 0.8274509803921568, 1.0)
color 5 (0.9921568627450981, 0.7058823529411765, 0.3843137254901961, 1.0)
color 6 (0.7019607843137254, 0.8705882352941177, 0.4117647058823529, 1.0)
color 7 (0.9882352941176471, 0.803921568627451, 0.8980392156862745, 1.0)
color 8 (0.8509803921568627, 0.8509803921568627, 0.8509803921568627, 1.0)
color 9 (0.7372549019607844, 0.5019607843137255, 0.7411764705882353, 1.0)
color 10 (0.8, 0.9215686274509803, 0.7725490196078432, 1.0)
color 11 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 12 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 13 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 14 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 15 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 16 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 17 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 18 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)
color 19 (1.0, 0.9294117647058824, 0.43529411764705883, 1.0)