调色板界的“扛把子”-palettable


有小伙伴说python可视化自带颜色很丑,那您一定是没遇到palettable,palettable是一个纯python写的颜色条(Colormap)库,汇集了大量知名可视化软件的颜色条(比如Tableau色系,matplotlib部分色系等等),共有颜色条(Colormap)1587类,可使用的单颜色数万种,本文详细介绍palettable如何使用。

本文目录


1、palettable快速安装

pip install palettable -i https://pypi.tuna.tsinghua.edu.cn/simple

2、palettable颜色条(Colormap)快速使用导入palettable包

import palettablefrom palettable.cartocolors.qualitative import Bold_9#为了描述方便,此处直接倒入palettable.cartocolors.qualitative大类下的Bold_9小类,#实际使用时可直接用palettable.cartocolors.qualitative.Bold_9

palettable重要属性-可视化色度条

Bold_9.show_discrete_image()#Bold_9各种颜色条图片

palettable重要属性-输出色度条中单颜色的数目

print(Bold_9.number)#Bold_9这种colormap中单颜色的数目

9即上面的图片有9格

palettable重要属性-输出色度条中单颜色的色号值

print(Bold_9.colors)#Bold_9 colormap中每种颜色的RGB格式色号print(Bold_9.hex_colors)#Bold_9 colormap中每种颜色的hex格式色号print(Bold_9.mpl_colors)#RGB tuples in the range 0-1 as used by matplotlib

[[127, 60, 141], [17, 165, 121], [57, 105, 172], [242, 183, 1], [231, 63, 116], [128, 186, 90], [230, 131, 16], [0, 134, 149], [207, 28, 144]]

['#7F3C8D', '#11A579', '#3969AC', '#F2B701', '#E73F74', '#80BA5A', '#E68310', '#008695', '#CF1C90']

[(0.4980392156862745, 0.23529411764705882, 0.5529411764705883), (0.06666666666666667, 0.6470588235294118, 0.4745098039215686), (0.2235294117647059, 0.4117647058823529, 0.6745098039215687), (0.9490196078431372, 0.7176470588235294, 0.00392156862745098), (0.9058823529411765, 0.24705882352941178, 0.4549019607843137), (0.5019607843137255, 0.7294117647058823, 0.35294117647058826), (0.9019607843137255, 0.5137254901960784, 0.06274509803921569), (0.0, 0.5254901960784314, 0.5843137254901961), (0.8117647058823529, 0.10980392156862745, 0.5647058823529412)]

Matplotlib中使用palettable

例子来自matplotlib-饼图(pie)

import matplotlib.pyplot as pltimport matplotlib as mplimport palettablempl.rc_file_defaults()my_dpi = 96plt.figure(figsize=(580 / my_dpi, 580 / my_dpi), dpi=my_dpi)plt.subplot(221)patches, texts, autotexts = plt.pie(    x=[1, 2, 3],    labels=['A', 'B', 'C'],    #使用palettable.tableau.BlueRed_6    colors=palettable.tableau.BlueRed_6.mpl_colors[0:3],    autopct='%.2f%%',    explode=(0.1, 0, 0))patches[0].set_alpha(0.3)patches[2].set_hatch('|')patches[1].set_hatch('x')plt.title('tableau.BlueRed_6', size=12)mpl.rc_file_defaults()plt.subplot(222)patches, texts, autotexts = plt.pie(    x=[1, 2, 3],    labels=['A', 'B', 'C'],    #使用palettable.cartocolors.qualitative.Bold_9    colors=palettable.cartocolors.qualitative.Bold_9.mpl_colors[0:3],    autopct='%.2f%%',    explode=(0.1, 0, 0))patches[0].set_alpha(0.3)patches[2].set_hatch('|')patches[1].set_hatch('x')plt.title('cartocolors.qualitative.Bold_9', size=12)mpl.rc_file_defaults()plt.subplot(223)patches, texts, autotexts = plt.pie(    x=[1, 2, 3],    labels=['A', 'B', 'C'],    #使用palettable.cartocolors.qualitative.Bold_9    colors=palettable.cartocolors.qualitative.Bold_9.mpl_colors[0:3],    autopct='%.2f%%',    explode=(0.1, 0, 0))patches[0].set_alpha(0.3)patches[2].set_hatch('|')patches[1].set_hatch('x')plt.title('cartocolors.qualitative.Bold_9', size=12)plt.subplot(223)patches, texts, autotexts = plt.pie(    x=[1, 2, 3],    labels=['A', 'B', 'C'],    #使用palettable.lightbartlein.sequential.Blues10_5    colors=palettable.lightbartlein.sequential.Blues10_5.mpl_colors[0:3],    autopct='%.2f%%',    explode=(0.1, 0, 0))#matplotlib.patches.Wedgepatches[0].set_alpha(0.3)patches[2].set_hatch('|')patches[1].set_hatch('x')plt.title('lightbartlein.sequential.Blues10_5', size=12)plt.subplot(224)patches, texts, autotexts = plt.pie(    x=[1, 2, 3],    labels=['A', 'B', 'C'],    colors=palettable.wesanderson.Moonrise5_6.mpl_colors[0:3],    autopct='%.2f%%',    explode=(0.1, 0, 0))patches[0].set_alpha(0.3)patches[2].set_hatch('|')patches[1].set_hatch('x')plt.title('wesanderson.Moonrise5_6', size=12)plt.show()

Seaborn中使用palettable

例子来自几行代码绘制靓丽矩阵图使用palettable.tableau.TrafficLight_9

import seaborn as snsiris_sns = sns.load_dataset("iris")import palettableg = sns.pairplot(    iris_sns,    hue='species',    palette=palettable.tableau.TrafficLight_9.mpl_colors,  #Matplotlib颜色)sns.set(style='whitegrid')g.fig.set_size_inches(10, 8)sns.set(style='whitegrid', font_scale=1.5)

使用palettable.tableau.BlueRed_6使用palettable.cartocolors.qualitative.Bold_9使用palettable.wesanderson.Moonrise5_6使用palettable.cartocolors.diverging.ArmyRose_7_r


3、palettable包含那些颜色条(Colormap)

palettable下面有16大类Colormap,共计1587小类Colormap,合计上万种单颜色可供使用,已经整理为pdf格式,感兴趣的可以包含以下16大类

palettable.cartocolors.divergingpalettable.cartocolors.qualitativepalettable.cartocolors.sequentialpalettable.cmocean.divergingpalettable.cmocean.sequentialpalettable.colorbrewer.divergingpalettable.colorbrewer.qualitativepalettable.colorbrewer.sequentialpalettable.lightbartlein.divergingpalettable.lightbartlein.sequentialpalettable.scientific.divergingpalettable.scientific.sequentialpalettable.matplotlibpalettable.mycartapalettable.tableaupalettable.wesanderson

共计1587小类【每个小类还有逆类,名称后面加“_r”即可】上面16大类下面有数个小类,例如,著名BI软件Tableau的配色条palettable.tableau这一大类,下面有palettable.tableau.BlueRed_12,palettable.tableau.GreenOrange_12等等数个小类:

palettable.tableau.BlueRed_12palettable.tableau.BlueRed_6palettable.tableau.ColorBlind_10palettable.tableau.Gray_5palettable.tableau.GreenOrange_12palettable.tableau.GreenOrange_6palettable.tableau.PurpleGray_12palettable.tableau.PurpleGray_6palettable.tableau.TableauLight_10palettable.tableau.TableauMedium_10palettable.tableau.Tableau_10palettable.tableau.Tableau_20palettable.tableau.TrafficLight_9

也就是类似上面的这种有1587行。


耿耿于怀着过去和忐忑不安着未来的人,也常常挥霍无度着现在。

调色板界的“扛把子”-palettable

相关文章:

你感兴趣的文章:

标签云: