我想达到的目标是:
我想在一个数字上创建几个饼图。他们都有一些共同的类别,但有时有不同的类别。显然,我希望所有相同的类别都有相同的颜色。
这就是为什么我创建了一本将类别(=标签)链接到颜色的字典。这样,我就可以指定饼图的颜色。但是,我想使用ggplot颜色(它随matplotlib.style.style.use(‘ggplot’)而来)。我怎样才能把这些颜色输入我的字典呢?
# set colors for labels
color_dict = {}
for i in range(0, len(data_categories)):
color_dict[data_categories[i]] = ???
# apply colors
ind_label = 0
for pie_wedge in pie[0]:
leg = ax[ind].get_legend()
pie_wedge.set_facecolor(color_dict[labels_0[ind_label]])
leg.legendHandles[ind_label].set_color_(color_dict[labels_0[ind_label]])
ind_label += 1发布于 2019-05-19 21:56:00
简短回答
要访问ggplot样式中使用的颜色,可以执行以下操作
In [37]: import matplotlib.pyplot as plt
In [38]: plt.style.use('ggplot')
In [39]: colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
In [40]: print('\n'.join(color for color in colors))
#E24A33
#348ABD
#988ED5
#777777
#FBC15E
#8EBA42
#FFB5B8在上面的示例中,颜色(如RGB字符串)包含在列表colors中。
请记住在访问颜色列表之前调用plt.style.use(...),否则您将找到标准颜色。
更详细的解释
上面的答案是为Matplotlib的现代版本量身定做的,其中的情节颜色和可能的其他情节属性,如行宽和破折号(请参阅this answer of mine)存储在rcParams字典中,键为'axes.prop_cycle',并包含在一种新的对象cycler中(在上面引用的答案中包含了对cycler的另一解释)。
要获得颜色列表,我们必须从cycler获取rcParams,然后使用它的.by_key()方法
签名: c.by_key() Docstring: Values,这将返回自行车者的转置值。在
Cycler上迭代生成每个键的一个值的dicts,该方法返回一个dictoflist,这是给定键的值。返回的值可用于仅使用Cycler创建等效的+。返回
要有一个值字典,最后,我们使用键'color'进行索引。
附录
严格来说,use('a_style')不需要访问其颜色,颜色(可能)是在存储在字典matplotlib.style.library中的matplotlib.RcParams对象中定义的。例如,让我们打印在不同样式中定义的所有颜色序列
In [23]: for style in plt.style.library:
...: the_rc = plt.style.library[style]
...: if 'axes.prop_cycle' in the_rc:
...: colors = the_rc['axes.prop_cycle'].by_key()['color']
...: print('%25s: %s'%(style, ', '.join(color for color in colors)))
...: else:
...: print('%25s: this style does not modify colors'%style)
dark_background: #8dd3c7, #feffb3, #bfbbd9, #fa8174, #81b1d2, #fdb462, #b3de69, #bc82bd, #ccebc4, #ffed6f
ggplot: #E24A33, #348ABD, #988ED5, #777777, #FBC15E, #8EBA42, #FFB5B8
seaborn-poster: this style does not modify colors
seaborn-talk: this style does not modify colors
seaborn-bright: #003FFF, #03ED3A, #E8000B, #8A2BE2, #FFC400, #00D7FF
seaborn-notebook: this style does not modify colors
seaborn-darkgrid: this style does not modify colors
bmh: #348ABD, #A60628, #7A68A6, #467821, #D55E00, #CC79A7, #56B4E9, #009E73, #F0E442, #0072B2
fast: this style does not modify colors
seaborn: #4C72B0, #55A868, #C44E52, #8172B2, #CCB974, #64B5CD
seaborn-white: this style does not modify colors
_classic_test: b, g, r, c, m, y, k
seaborn-deep: #4C72B0, #55A868, #C44E52, #8172B2, #CCB974, #64B5CD
seaborn-paper: this style does not modify colors
grayscale: 0.00, 0.40, 0.60, 0.70
seaborn-dark-palette: #001C7F, #017517, #8C0900, #7600A1, #B8860B, #006374
seaborn-colorblind: #0072B2, #009E73, #D55E00, #CC79A7, #F0E442, #56B4E9
tableau-colorblind10: #006BA4, #FF800E, #ABABAB, #595959, #5F9ED1, #C85200, #898989, #A2C8EC, #FFBC79, #CFCFCF
seaborn-dark: this style does not modify colors
classic: b, g, r, c, m, y, k
seaborn-pastel: #92C6FF, #97F0AA, #FF9F9A, #D0BBFF, #FFFEA3, #B0E0E6
seaborn-ticks: this style does not modify colors
Solarize_Light2: #268BD2, #2AA198, #859900, #B58900, #CB4B16, #DC322F, #D33682, #6C71C4
seaborn-whitegrid: this style does not modify colors
seaborn-muted: #4878CF, #6ACC65, #D65F5F, #B47CC7, #C4AD66, #77BEDB
fivethirtyeight: #008fd5, #fc4f30, #e5ae38, #6d904f, #8b8b8b, #810f7cPS -据我理解
seaborn-xxx样式将用作样式序列中的最后一步,例如plt.style.use(['seaborn', 'seaborn-poster'])或plt.style.use(['seaborn', 'seaborn-muted', 'seaborn-poster'])fast,就是调整呈现参数以获得更快的呈现。https://stackoverflow.com/questions/56211675
复制相似问题