多个matplotlib pyplot等高线图的颜色循环

时间:2017-07-05 05:42:10

标签: python matplotlib plot colors

可以使用matplotlib.pyplot中的基本图表循环线条颜色:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle(plt.cycler('color', ['c', 'm', 'y', 'k']))

x = np.linspace(-1.0, 1.0, 50)
for f in [1.0, 2.0, 3.0, 4.0]:
    ax.plot(x, np.sin(x * f))
plt.show()

这导致每个部分正弦波图具有来自循环器列表的下一个颜色,并且它将根据需要进行换行:

Figure 1

我使用contour绘图来绘制单个轮廓。对于每种情况,我想绘制一个轮廓但我希望显示的颜色自动循环通过指定的调色板(所以我可以显示一个可读的颜色编码的图例)。但是,ax.set_prop_cycle似乎没有相同的效果:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle(plt.cycler('color', ['c', 'm', 'y', 'k']))

x = np.linspace(-1.0, 1.0, 50)
y = np.linspace(-1.0, 1.0, 50)
z = np.zeros((len(x), len(y)))

# simple function to create useful contours:
def get_z(x, y, f):
    for i, u in enumerate(x):
        for j, v in enumerate(y):
            z[i, j] = (f * u) ** 2 + (f * v) ** 2
    return z

# plot for multiple values of `f`
for f in [1.0, 2.0, 3.0, 4.0]:
    ax.contour(x, y, get_z(x, y, f), levels=[1], linewidth=2)
plt.show()

Figure 2

每个图(环)都具有相同的颜色 - 它没有自动循环。我想让每个情节都有不同的颜色。我知道每个"完整"等高线绘制作为单个绘图的一部分绘制的每个轮廓的颜色循环,但在我的情况下,我只是在固定的"水平"绘制单个轮廓。

如果没有为每个情节明确指定颜色,是否有一种相当简单的方法可以做到这一点?我最终计划绘制一个动态数量的图,所以将它们全部指定并不是很实际,我需要它在必要时包装。

也许有办法设置或旋转轮廓图的颜色图的起始偏移量?

2 个答案:

答案 0 :(得分:2)

您可以在colors中指定关键字plt.contour。我不知道这是否是最好的解决方案,但是我为你的颜色值编写了一个小的循环器函数,这样我就可以在plt.contour - 循环的每次迭代时将它们提供给for

from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
##ax.set_prop_cycle(plt.cycler('color', ['c', 'm', 'y', 'k']))

x = np.linspace(-1.0, 1.0, 50)
y = np.linspace(-1.0, 1.0, 50)
z = np.zeros((len(x), len(y)))

# simple function to create useful contours:
def get_z(x, y, f):
    for i, u in enumerate(x):
        for j, v in enumerate(y):
            z[i, j] = (f * u) ** 2 + (f * v) ** 2
    return z

def col_cycler(cols):
    count = 0
    while True:
        yield cols[count]
        count = (count + 1)%len(cols)

# plot for multiple values of `f`
col_iter = col_cycler(['c','m', 'y','k'])
for f in [1.0, 2.0, 3.0, 4.0, 5.0]:
    ax.contour(x, y, get_z(x, y, f), levels=[1], linewidth=2, colors=next(col_iter))
plt.show()

结果如下:

contour plots

在Python 3.5上测试

答案 1 :(得分:1)

由于你无论如何都要遍历你的参数,你可能只是同时循环颜色。

# plot for multiple values of `f`
for f, c in zip([1.0, 2.0, 3.0, 4.0], ['c', 'm', 'y', 'k']):
    ax.contour(x, y, get_z(x, y, f), levels=[1], linewidth=2, colors=c)

或者,如果图的数量未知,

colors = ['c', 'm', 'y', 'k']
for i,f in enumerate([1.0, 2.0, 3.0, 4.0]):
    ax.contour(x, y, get_z(x, y, f), levels=[1], linewidth=2, colors=colors[i%len(colors)])
plt.show()

因此,如果没有使用循环,那么不希望“明确指定每个绘图的颜色”的论点才有意义。

因此,为了在没有循环的情况下获得相同的结果,您可以为同一轮廓图指定多个级别并使用色彩映射。

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

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.linspace(-1.0, 1.0, 50)
y = np.linspace(-1.0, 1.0, 50)
z = np.zeros((len(x), len(y)))

# simple function to create useful contours:
def get_z(x, y, f):
    for i, u in enumerate(x):
        for j, v in enumerate(y):
            z[i, j] = (f * u) ** 2 + (f * v) ** 2
    return z

colors = ['c', 'm', 'y', 'k']
levels = 1./np.array([1.0, 2.0, 3.0, 4.0][::-1])**2
cmcol =  zip(plt.Normalize(levels.min(), levels.max())(levels),colors[::-1])

cmap=matplotlib.colors.LinearSegmentedColormap.from_list("m", cmcol)
cont = ax.contour(x, y, get_z(x, y, 1), levels=levels, linewidth=2, cmap=cmap)

plt.show()

enter image description here