如何在 matplotlib 绘图循环中为标记和线条设置相同的颜色? [英] How to set same color for markers and lines in a matplotlib plot loop?

查看:78
本文介绍了如何在 matplotlib 绘图循环中为标记和线条设置相同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过创建循环来使用matplotlib绘制多条线和标记,并且已经在matplolibrc参数文件中设置了轴的颜色循环.在循环的每个循环中,都会创建一组标记和线条(线条由单独的命令创建).但是,标记和线条颜色随轴颜色周期而不同.我希望每次运行循环时,标记和线条都具有与该循环相同的颜色.

I have to plot multiple lines and markers with matplotlib by creating a loop and I have already set the axes color cycle in the matplolibrc param file. In each cycle of the loop a set of markers and lines are created (lines are created by a separate command). But the marker and line colors are different as per the axes color cycle. I want each time the loop is run the marker and lines to be of the same color of that cycle.

我已包含可复制的代码:

I have included the reproducible code:

import numpy as np
import itertools
import matplotlib.pyplot as plt
m = 5
n = 5
x = np.zeros(shape=(m, n))
plt.figure(figsize=(5.15, 5.15))
plt.clf()
plt.subplot(111)
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next())
    plt.plot(x, y, linestyle='-')
plt.ylabel(r'\textit{y}', labelpad=6)
plt.xlabel(r'\textit{x}', labelpad=6)
plt.show()

使用此代码,我得到的输出是:

Using this code the output I get is:

我需要在其范围内绘制的标记和线条具有相同的颜色.如何在matplotlib中完成?

I need the markers and lines plotted in their range to be of the same color. How can it be done in matplotlib?

推荐答案

我提出了两种方法.第一个可能更清晰:它循环一次,在每个循环中,获取下一种颜色,然后使用该颜色执行两个绘图命令.在第二个中,它循环并执行所有标记,然后重置颜色并再次循环并执行线条.

I present two ways. The first is probably cleaner: it loops through once, and within each loop, gets the next color, and then does two plotting commands with that color. In the second, it loops through and does all the markers and then it resets the colors and loops through again and does the lines.

第一种方法

直接访问颜色循环.如果 matplotlib 1.5 或更高版本使用 color=next(ax._get_lines.prop_cycler)['color'].否则 color = next(ax._get_lines.color_cycle):

Directly access the color cycle. If matplotlib 1.5 or later use color=next(ax._get_lines.prop_cycler)['color']. Otherwise color=next(ax._get_lines.color_cycle):

import numpy as np
import itertools
import matplotlib.pyplot as plt
m = 5
n = 5
x = np.zeros(shape=(m, n))
plt.figure(figsize=(5.15, 5.15))
plt.clf()
plt.subplot(111)
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))

ax = plt.gca()
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    #
    #for matplotlib before 1.5, use
    #color = next(ax._get_lines.color_cycle)
    #instead of next line (thanks to Jon Loveday for the update)
    #
    color = next(ax._get_lines.prop_cycler)['color']
    plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next(), color=color)
    plt.plot(x, y, linestyle='-', color = color)
plt.ylabel(r'$y$', labelpad=6)
plt.xlabel(r'$x$', labelpad=6)
plt.savefig('test2.png')

另外,请注意我将您的 \textit{y} 更改为 $y$.通常,您实际上想要的是数学字体而不是斜体.

Also, notice I changed your \textit{y} to $y$. Usually you actually want math fonts rather than italics.

第二种方法

如果您不想将颜色作为参数发送,您可以重置颜色循环并循环两次.在这里,我在第一个循环之前重置它,然后在第二个循环之前再次重置它,以确保它从同一个地方开始.

If you don't want to send a color in as an argument, you can reset the color cycle and go through the loop twice. Here I reset it before the first loop and then again before the second loop just to make sure it's starting from the same place.

import numpy as np
import itertools
import matplotlib.pyplot as plt
m = 5
n = 5
x = np.zeros(shape=(m, n))
plt.figure(figsize=(5.15, 5.15))
plt.clf()
plt.subplot(111)
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))

plt.gca().set_prop_cycle(None) #if matplotlib <1.5 use set_color_cycle
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next())
plt.gca().set_prop_cycle(None)
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    plt.plot(x, y, linestyle='-')
plt.ylabel(r'$y$', labelpad=6)
plt.xlabel(r'$x$', labelpad=6)
plt.savefig('test.png')

这篇关于如何在 matplotlib 绘图循环中为标记和线条设置相同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆