栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从matplotlib样式获取颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从matplotlib样式获取颜色

简短答案

要访问

ggplot
样式中使用的颜色,可以执行以下操作

In [37]: import matplotlib.pyplot as pltIn [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字符串)包含在list中

colors

plt.style.use(...)
在访问颜色列表之前,请记住先致电,否则您将找到标准颜色。


更详细的解释

上面的答案是针对Matplotlib的现代发行版量身定制的,在该版本中,绘图颜色和其他可能的绘图属性(如线宽和破折号(请参阅我的答案))

rcParams
使用键存储在字典中,
'axes.prop_cycle'
并包含在一种新的对象中,a
cycler
cycler
上面引用的我的回答中包含a的其他说明)。

为了获得颜色列表中,我们必须得到

cycler
rcParams
,然后利用其
.by_key()
方法

Signature: c.by_key()Docstring: Values by keyThis returns the transposed values of the cycler.  Iteratingover a `Cycler` yields dicts with a single value for each key,this method returns a `dict` of `list` which are the valuesfor the given key.The returned value can be used to create an equivalent `Cycler`using only `+`.Returns-------transpose : dict    dict of lists of the values for each key.

拥有一个值字典,最后,我们使用key进行索引

'color'


附录

并非绝对必须

use('a_style')
访问其颜色,颜色(可能)
matplotlib.RcParams
是在存储在字典中的对象中定义的
matplotlib.style.library
。例如,让我们打印以不同样式定义的所有颜色序列

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, #FFB5B8seaborn-poster: this style does not modify colors  seaborn-talk: this style does not modify colorsseaborn-bright: #003FFF, #03ED3A, #E8000B, #8A2BE2, #FFC400, #00D7FF         seaborn-notebook: this style does not modify colors         seaborn-darkgrid: this style does not modify colorsbmh: #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, kseaborn-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, #810f7c

PS-以我的理解

  • seaborn-xxx
    不修改颜色样式在样式序列来作为最后一步,例如,
    plt.style.use(['seaborn', 'seaborn-poster'])
    plt.style.use(['seaborn', 'seaborn-muted', 'seaborn-poster'])
  • 唯一
    fast
    不更改颜色的样式就是调整渲染参数以实现 更快的 渲染。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号