使用matplotlib颜色图进行颜色循环 - python

如果我通过以下方式创建颜色:
将numpy导入为np
从matplotlib导入pyplot作为plt

n = 6
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n))
color

color是一个numpy数组:

array([[ 0.34832334, 0.46571115, 0.88834616, 1. ],
[ 0.56518158, 0.69943844, 0.99663507, 1. ],
[ 0.77737753, 0.84092121, 0.9461493 , 1. ],
[ 0.93577377, 0.8122367 , 0.74715647, 1. ],
[ 0.96049006, 0.61627642, 0.4954666 , 1. ],
[ 0.83936494, 0.32185622, 0.26492398, 1. ]])

但是,如果我在.mplstyle文件(map(tuple,color[:,0:-1]))中将RGB值(没有alpha值1)插入为元组,则会收到类似于以下错误:

in file "/home/moritz/.config/matplotlib/stylelib/ggplot.mplstyle"
Key axes.color_cycle: [(0.34832334141176474 does not look like a color arg
(val, error_details, msg))

任何想法为什么?

参考方案

详细信息实际上在matplotlibrc本身中:它需要一个字符串rep(十六进制或字母或单词,而不是元组)。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

fig, ax1 = plt.subplots(1,1)

ys = np.random.random((5, 6))
ax1.plot(range(5), ys)
ax1.set_title('Default color cycle')
plt.show()

# From the sample matplotlibrc:
#axes.color_cycle    : b, g, r, c, m, y, k  # color cycle for plot lines
                                            # as list of string colorspecs:
                                            # single letter, long name, or
                                            # web-style hex

# setting color cycle after calling plt.subplots doesn't "take"
# try some hex values as **string** colorspecs
mpl.rcParams['axes.color_cycle'] = ['#129845','#271254', '#FA4411', '#098765', '#000009']

fig, ax2 = plt.subplots(1,1)
ax2.plot(range(5), ys)
ax2.set_title('New color cycle')


n = 6
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n)) # This returns RGBA; convert:
hexcolor = map(lambda rgb:'#%02x%02x%02x' % (rgb[0]*255,rgb[1]*255,rgb[2]*255),
               tuple(color[:,0:-1]))

mpl.rcParams['axes.color_cycle'] = hexcolor

fig, ax3 = plt.subplots(1,1)
ax3.plot(range(5), ys)
ax3.set_title('Color cycle from colormap')

plt.show()

Matplotlib'粗体'字体 - python

跟随this example:import numpy as np import matplotlib.pyplot as plt fig = plt.figure() for i, label in enumerate(('A', 'B', 'C', 'D')): ax = f…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…