且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取matplotlib颜色循环状态

更新时间:2023-01-27 12:44:46

访问颜色循环迭代器

没有用于访问底层迭代器的面向用户"(也称为公共")方法,但是您可以通过专用"(按惯例)方法来访问它.但是,如果不更改iterator的状态,就无法获取.

Accessing the color cycle iterator

There's no "user-facing" (a.k.a. "public") method to access the underlying iterator, but you can access it through "private" (by convention) methods. However, you'd can't get the state of an iterator without changing it.

快速使用:您可以通过多种方式设置颜色/属性周期(例如,版本ax.set_color_cycle或版本> = 1.5的ax.set_prop_cycler).请查看此处的1.5版或更高版本的示例,或此处的先前样式.

Quick aside: You can set the color/property cycle in a variety of ways (e.g. ax.set_color_cycle in versions <1.5 or ax.set_prop_cycler in >=1.5). Have a look at the example here for version 1.5 or greater, or the previous style here.

但是,虽然没有面向公众的方法来访问可迭代对象,但是您可以通过_get_lines辅助类实例为给定的axis对象(ax)访问它. ax._get_lines是一个易混淆的名称,但是它是一种幕后机制,它允许plot命令处理所有可以调用plot的奇特和变化的方式.除其他事项外,它还能跟踪自动分配的颜色.同样,可以使用ax._get_patches_for_fill通过默认的填充颜色和色块属性控制循环.

However, while there's no public-facing method to access the iterable, you can access it for a given axes object (ax) through the _get_lines helper class instance. ax._get_lines is a touch confusingly named, but it's the behind-the-scenes machinery that allows the plot command to process all of the odd and varied ways that plot can be called. Among other things, it's what keeps track of what colors to automatically assign. Similarly, there's ax._get_patches_for_fill to control cycling through default fill colors and patch properties.

无论如何,行的颜色循环可迭代为ax._get_lines.color_cycle,而色块的可迭代颜色为ax._get_patches_for_fill.color_cycle.在matplotlib> = 1.5上,它已更改为使用cycler,并且可迭代项称为而不是color_cycle并产生属性的dict而不是颜色.

At any rate, the color cycle iterable is ax._get_lines.color_cycle for lines and ax._get_patches_for_fill.color_cycle for patches. On matplotlib >=1.5, this has changed to use the cycler library, and the iterable is called prop_cycler instead of color_cycle and yields a dict of properties instead of only a color.

总而言之,您将执行以下操作:

All in all, you'd do something like:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
color_cycle = ax._get_lines.color_cycle
# or ax._get_lines.prop_cycler on version >= 1.5
# Note that prop_cycler cycles over dicts, so you'll want next(cycle)['color']

您无法查看iterator

的状态

但是,该对象是裸" iterator.我们可以很容易地得到下一个项目(例如next_color = next(color_cycle),但这意味着之后的下一个颜色 是要绘制的颜色.通过设计,如果没有迭代器,就无法获得迭代器的当前状态.更改它.

You can't view the state of an iterator

However, this object is a "bare" iterator. We can easily get the next item (e.g. next_color = next(color_cycle), but that means that the next color after that is what will be plotted. By design, there's no way to get the current state of an iterator without changing it.

v1.5或更高版本中,***获取使用的cycler对象,因为我们可以推断出它的当前状态.但是,cycler对象本身无法在任何地方(公共或私有)访问.而是只能访问从cycler对象创建的itertools.cycle实例.无论哪种方式,都无法获得颜色/属性循环器的基本状态.

In v1.5 or greater, it would be nice to get the cycler object that's used, as we could infer its current state. However, the cycler object itself isn't accessible (publicly or privately) anywhere. Instead, only the itertools.cycle instance created from the cycler object is accessible. Either way, there's no way to get to the underlying state of the color/property cycler.

在您的情况下,听起来好像您要匹配刚刚绘制的内容的颜色.与其尝试确定颜色/属性是什么,不如根据绘图的属性设置新项目的颜色/等.

In your case, it sounds like you're wanting to match the color of something that was just plotted. Instead of trying to determine what the color/property will be, set the color/etc of your new item based on the properties of what's plotted.

例如,在您描述的情况下,我将执行以下操作:

For example, in the case you described, I'd do something like this:

import matplotlib.pyplot as plt
import numpy as np

def custom_plot(x, y, **kwargs):
    ax = kwargs.pop('ax', plt.gca())
    base_line, = ax.plot(x, y, **kwargs)
    ax.fill_between(x, 0.9*y, 1.1*y, facecolor=base_line.get_color(), alpha=0.5)

x = np.linspace(0, 1, 10)
custom_plot(x, x)
custom_plot(x, 2*x)
custom_plot(x, -x, color='yellow', lw=3)

plt.show()

在这种情况下,这不是唯一的方法,但是它比尝试预先获得绘图线的颜色更干净.

It's not the only way, but its cleaner than trying to get the color of the plotted line before-hand, in this case.