你是否在Python绘制图表时,纠结过要使用哪种颜色,是否也曾遇到知道应该使用哪种颜色,却无奈于不知道颜色编码的情况。接下来简单介绍一下Python当中涉及的156种颜色编码及颜色名称。

1、在Python当中,首先调用matplotlib函数。

import matplotlib.pyplot as pltfrom matplotlib import colors as mcolors

2、获取所有颜色名称和对应的RGB值

# 获取所有颜色名称和对应的RGB值colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)print(colors)

结果:

颜色名称颜色编码颜色名称颜色编码颜色名称颜色编码颜色名称颜色编码
b(0,0,1)darkorchid#9932CClightgoldenrodyellow#FAFAD2palegreen#98FB98
g(0,0.5,0)darkred#8B0000lightgray#D3D3D3paleturquoise#AFEEEE
r(1,0,0)darksalmon#E9967Alightgreen#90EE90palevioletred#DB7093
c(0,0.75,0.75)darkseagreen#8FBC8Flightgrey#D3D3D3papayawhip#FFEFD5
m(0.75,0,0.75)darkslateblue#483D8Blightpink#FFB6C1peachpuff#FFDAB9
y(0.75,0.75,0)darkslategray#2F4F4Flightsalmon#FFA07Aperu#CD853F
k(0,0,0)darkslategrey#2F4F4Flightseagreen#20B2AApink#FFC0CB
w(1,1,1)darkturquoise#00CED1lightskyblue#87CEFAplum#DDA0DD
aliceblue#F0F8FFdarkviolet#9400D3lightslategray#778899powderblue#B0E0E6
antiquewhite#FAEBD7deeppink#FF1493lightslategrey#778899purple#800080
aqua#00FFFFdeepskyblue#00BFFFlightsteelblue#B0C4DErebeccapurple#663399
aquamarine#7FFFD4dimgray#696969lightyellow#FFFFE0red#FF0000
azure#F0FFFFdimgrey#696969lime#00FF00rosybrown#BC8F8F
beige#F5F5DCdodgerblue#1E90FFlimegreen#32CD32royalblue#4169E1
bisque#FFE4C4firebrick#B22222linen#FAF0E6saddlebrown#8B4513
black#000000floralwhite#FFFAF0magenta#FF00FFsalmon#FA8072
blanchedalmond#FFEBCDforestgreen#228B22maroon#800000sandybrown#F4A460
blue#0000FFfuchsia#FF00FFmediumaquamarine#66CDAAseagreen#2E8B57
blueviolet#8A2BE2gainsboro#DCDCDCmediumblue#0000CDseashell#FFF5EE
brown#A52A2Aghostwhite#F8F8FFmediumorchid#BA55D3sienna#A0522D
burlywood#DEB887gold#FFD700mediumpurple#9370DBsilver#C0C0C0
cadetblue#5F9EA0goldenrod#DAA520mediumseagreen#3CB371skyblue#87CEEB
chartreuse#7FFF00gray#808080mediumslateblue#7B68EEslateblue#6A5ACD
chocolate#D2691Egreen#008000mediumspringgreen#00FA9Aslategray#708090
coral#FF7F50greenyellow#ADFF2Fmediumturquoise#48D1CCslategrey#708090
cornflowerblue#6495EDgrey#808080mediumvioletred#C71585snow#FFFAFA
cornsilk#FFF8DChoneydew#F0FFF0midnightblue#191970springgreen#00FF7F
crimson#DC143Chotpink#FF69B4mintcream#F5FFFAsteelblue#4682B4
cyan#00FFFFindianred#CD5C5Cmistyrose#FFE4E1tan#D2B48C
darkblue#00008Bindigo#4B0082moccasin#FFE4B5teal#008080
darkcyan#008B8Bivory#FFFFF0navajowhite#FFDEADthistle#D8BFD8
darkgoldenrod#B8860Bkhaki#F0E68Cnavy#000080tomato#FF6347
darkgray#A9A9A9lavender#E6E6FAoldlace#FDF5E6turquoise#40E0D0
darkgreen#006400lavenderblush#FFF0F5olive#808000violet#EE82EE
darkgrey#A9A9A9lawngreen#7CFC00olivedrab#6B8E23wheat#F5DEB3
darkkhaki#BDB76Blemonchiffon#FFFACDorange#FFA500white#FFFFFF
darkmagenta#8B008Blightblue#ADD8E6orangered#FF4500whitesmoke#F5F5F5
darkolivegreen#556B2Flightcoral#F08080orchid#DA70D6yellow#FFFF00
darkorange#FF8C00lightcyan#E0FFFFpalegoldenrod#EEE8AAyellowgreen#9ACD32

3、绘图展示所有颜色及名称

import matplotlib.pyplot as pltfrom matplotlib import colors as mcolors# 获取所有颜色名称和对应的RGB值colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)# 创建图表,设置大小fig, ax = plt.subplots(figsize=(10, 8))# 设置坐标轴不可见ax.set_axis_off()# 设置标题ax.set_title('Python中的颜色对比', fontsize=16, fontweight='bold')# 用来正常显示中文标签plt.rcParams['font.sans-serif'] = ['SimHei']# 设置颜色样本的位置num_colors = len(colors)num_rows = num_colors // 4 + 1# 计算每个颜色样本的宽度和高度sample_width = 1.0 / 4sample_height = 1.0 / num_rows# 遍历所有颜色并绘制颜色样本for i, (color_name, color_rgb) in enumerate(colors.items()):col = i % 4row = i // 4x = col * sample_widthy = 1 - (row + 1) * sample_heightax.add_patch(plt.Rectangle((x, y), sample_width, sample_height,facecolor=color_rgb, edgecolor='black'))ax.text(x + 0.5 * sample_width, y + 0.5 * sample_height, color_name,color='black', ha='center', va='center', fontsize=12)plt.show()

颜色结果展示:

4、绘图展示所有颜色及颜色编码

import matplotlib.pyplot as pltfrom matplotlib import colors as mcolors# 获取所有颜色名称和对应的RGB值colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)# 创建图表,设置大小fig, ax = plt.subplots(figsize=(10, 8))# 设置坐标轴不可见ax.set_axis_off()# 设置标题ax.set_title('Python中的颜色编码对比', fontsize=16, fontweight='bold')# 用来正常显示中文标签plt.rcParams['font.sans-serif'] = ['SimHei']# 设置颜色样本的位置num_colors = len(colors)num_rows = num_colors // 4 + 1# 计算每个颜色样本的宽度和高度sample_width = 1.0 / 4sample_height = 1.0 / num_rows# 遍历所有颜色并绘制颜色样本for i, (color_code, color_rgb) in enumerate(colors.items()):col = i % 4row = i // 4x = col * sample_widthy = 1 - (row + 1) * sample_heightax.add_patch(plt.Rectangle((x, y), sample_width, sample_height,facecolor=color_rgb, edgecolor='black'))# 将RGB值转换为十六进制格式hex_color = mcolors.to_hex(color_rgb)ax.text(x + 0.5 * sample_width, y + 0.5 * sample_height, hex_color,color='black', ha='center', va='center', fontsize=12)plt.show()

颜色及编码结果展示:

以上就是Python当中matplotlib函数绘图时遇到的所有颜色,希望对你有帮助!

还想学习Python的哪些重要知识,留言哦!!!