你是否在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 | #9932CC | lightgoldenrodyellow | #FAFAD2 | palegreen | #98FB98 |
g | (0,0.5,0) | darkred | #8B0000 | lightgray | #D3D3D3 | paleturquoise | #AFEEEE |
r | (1,0,0) | darksalmon | #E9967A | lightgreen | #90EE90 | palevioletred | #DB7093 |
c | (0,0.75,0.75) | darkseagreen | #8FBC8F | lightgrey | #D3D3D3 | papayawhip | #FFEFD5 |
m | (0.75,0,0.75) | darkslateblue | #483D8B | lightpink | #FFB6C1 | peachpuff | #FFDAB9 |
y | (0.75,0.75,0) | darkslategray | #2F4F4F | lightsalmon | #FFA07A | peru | #CD853F |
k | (0,0,0) | darkslategrey | #2F4F4F | lightseagreen | #20B2AA | pink | #FFC0CB |
w | (1,1,1) | darkturquoise | #00CED1 | lightskyblue | #87CEFA | plum | #DDA0DD |
aliceblue | #F0F8FF | darkviolet | #9400D3 | lightslategray | #778899 | powderblue | #B0E0E6 |
antiquewhite | #FAEBD7 | deeppink | #FF1493 | lightslategrey | #778899 | purple | #800080 |
aqua | #00FFFF | deepskyblue | #00BFFF | lightsteelblue | #B0C4DE | rebeccapurple | #663399 |
aquamarine | #7FFFD4 | dimgray | #696969 | lightyellow | #FFFFE0 | red | #FF0000 |
azure | #F0FFFF | dimgrey | #696969 | lime | #00FF00 | rosybrown | #BC8F8F |
beige | #F5F5DC | dodgerblue | #1E90FF | limegreen | #32CD32 | royalblue | #4169E1 |
bisque | #FFE4C4 | firebrick | #B22222 | linen | #FAF0E6 | saddlebrown | #8B4513 |
black | #000000 | floralwhite | #FFFAF0 | magenta | #FF00FF | salmon | #FA8072 |
blanchedalmond | #FFEBCD | forestgreen | #228B22 | maroon | #800000 | sandybrown | #F4A460 |
blue | #0000FF | fuchsia | #FF00FF | mediumaquamarine | #66CDAA | seagreen | #2E8B57 |
blueviolet | #8A2BE2 | gainsboro | #DCDCDC | mediumblue | #0000CD | seashell | #FFF5EE |
brown | #A52A2A | ghostwhite | #F8F8FF | mediumorchid | #BA55D3 | sienna | #A0522D |
burlywood | #DEB887 | gold | #FFD700 | mediumpurple | #9370DB | silver | #C0C0C0 |
cadetblue | #5F9EA0 | goldenrod | #DAA520 | mediumseagreen | #3CB371 | skyblue | #87CEEB |
chartreuse | #7FFF00 | gray | #808080 | mediumslateblue | #7B68EE | slateblue | #6A5ACD |
chocolate | #D2691E | green | #008000 | mediumspringgreen | #00FA9A | slategray | #708090 |
coral | #FF7F50 | greenyellow | #ADFF2F | mediumturquoise | #48D1CC | slategrey | #708090 |
cornflowerblue | #6495ED | grey | #808080 | mediumvioletred | #C71585 | snow | #FFFAFA |
cornsilk | #FFF8DC | honeydew | #F0FFF0 | midnightblue | #191970 | springgreen | #00FF7F |
crimson | #DC143C | hotpink | #FF69B4 | mintcream | #F5FFFA | steelblue | #4682B4 |
cyan | #00FFFF | indianred | #CD5C5C | mistyrose | #FFE4E1 | tan | #D2B48C |
darkblue | #00008B | indigo | #4B0082 | moccasin | #FFE4B5 | teal | #008080 |
darkcyan | #008B8B | ivory | #FFFFF0 | navajowhite | #FFDEAD | thistle | #D8BFD8 |
darkgoldenrod | #B8860B | khaki | #F0E68C | navy | #000080 | tomato | #FF6347 |
darkgray | #A9A9A9 | lavender | #E6E6FA | oldlace | #FDF5E6 | turquoise | #40E0D0 |
darkgreen | #006400 | lavenderblush | #FFF0F5 | olive | #808000 | violet | #EE82EE |
darkgrey | #A9A9A9 | lawngreen | #7CFC00 | olivedrab | #6B8E23 | wheat | #F5DEB3 |
darkkhaki | #BDB76B | lemonchiffon | #FFFACD | orange | #FFA500 | white | #FFFFFF |
darkmagenta | #8B008B | lightblue | #ADD8E6 | orangered | #FF4500 | whitesmoke | #F5F5F5 |
darkolivegreen | #556B2F | lightcoral | #F08080 | orchid | #DA70D6 | yellow | #FFFF00 |
darkorange | #FF8C00 | lightcyan | #E0FFFF | palegoldenrod | #EEE8AA | yellowgreen | #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的哪些重要知识,留言哦!!!