matplotlib-黑色&白色颜色图(带有破折号,点等) [英] matplotlib - black & white colormap (with dashes, dots etc)

查看:103
本文介绍了matplotlib-黑色&白色颜色图(带有破折号,点等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib创建2d线图.出于发布的目的,我希望将这些图显示为黑白(灰度),而我正努力寻找一种非侵入式的解决方案.

I am using matplotlib to create 2d line-plots. For the purposes of publication, I would like to have those plots in black and white (not grayscale), and I am struggling to find a non-intrusive solution for that.

Gnuplot会自动更改不同行的虚线图案,使用matplotlib可能会发生类似情况吗?

Gnuplot automatically alters dashing patterns for different lines, is something similar possible with matplotlib?

推荐答案

下面,我提供了一些功能,可以将彩色线条转换为具有独特风格的黑色线条.我的快速测试显示,经过7行,颜色重复出现.如果不是这种情况(我弄错了),则需要对提供的例程中的常量" COLORMAP进行细微调整.

Below I provide functions to convert a colored line to a black line with unique style. My quick test showed that after 7 lines, the colors repeated. If this is not the case (and I made a mistake), then a minor adjustment is needed for the "constant" COLORMAP in the provided routine.

这是例程和示例:

import matplotlib.pyplot as plt
import numpy as np

def setAxLinesBW(ax):
    """
    Take each Line2D in the axes, ax, and convert the line style to be 
    suitable for black and white viewing.
    """
    MARKERSIZE = 3

    COLORMAP = {
        'b': {'marker': None, 'dash': (None,None)},
        'g': {'marker': None, 'dash': [5,5]},
        'r': {'marker': None, 'dash': [5,3,1,3]},
        'c': {'marker': None, 'dash': [1,3]},
        'm': {'marker': None, 'dash': [5,2,5,2,5,10]},
        'y': {'marker': None, 'dash': [5,3,1,2,1,10]},
        'k': {'marker': 'o', 'dash': (None,None)} #[1,2,1,10]}
        }


    lines_to_adjust = ax.get_lines()
    try:
        lines_to_adjust += ax.get_legend().get_lines()
    except AttributeError:
        pass

    for line in lines_to_adjust:
        origColor = line.get_color()
        line.set_color('black')
        line.set_dashes(COLORMAP[origColor]['dash'])
        line.set_marker(COLORMAP[origColor]['marker'])
        line.set_markersize(MARKERSIZE)

def setFigLinesBW(fig):
    """
    Take each axes in the figure, and for each line in the axes, make the
    line viewable in black and white.
    """
    for ax in fig.get_axes():
        setAxLinesBW(ax)

xval = np.arange(100)*.01

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

ax.plot(xval,np.cos(2*np.pi*xval))
ax.plot(xval,np.cos(3*np.pi*xval))
ax.plot(xval,np.cos(4*np.pi*xval))
ax.plot(xval,np.cos(5*np.pi*xval))
ax.plot(xval,np.cos(6*np.pi*xval))
ax.plot(xval,np.cos(7*np.pi*xval))
ax.plot(xval,np.cos(8*np.pi*xval))

ax = fig.add_subplot(212)
ax.plot(xval,np.cos(2*np.pi*xval))
ax.plot(xval,np.cos(3*np.pi*xval))
ax.plot(xval,np.cos(4*np.pi*xval))
ax.plot(xval,np.cos(5*np.pi*xval))
ax.plot(xval,np.cos(6*np.pi*xval))
ax.plot(xval,np.cos(7*np.pi*xval))
ax.plot(xval,np.cos(8*np.pi*xval))

fig.savefig("colorDemo.png")
setFigLinesBW(fig)
fig.savefig("bwDemo.png")

这提供了以下两个图: 首先是颜色: 然后以黑白显示:

This provides the following two plots: First in color: Then in black and white:

您可以调整每种颜色转换为样式的方式.如果您只想使用破折号样式(-.vs.-vs.您想要的任何样式),请将COLORMAP对应的'marker'值设置为None并调整'dash'样式,否则反之亦然.

You can adjust how each color is converted to a style. If you just want to only play with the dash style (-. vs. -- vs. whatever pattern you want), set the COLORMAP corresponding 'marker' value to None and adjusted the 'dash' pattern, or vice versa.

例如,词典中的最后一个颜色是'k'(黑色);最初,我只有一个虚线图案[1,2,1,10],对应于所示的一个像素,两个未显示,一个未显示,10个未显示,这是一个点-点-空格图案.然后我将其注释掉,将破折号设置为(None,None),这是一种非常正式的实线表达方式,并在圆圈上添加了标记"o".

For example, the last color in the dictionary is 'k' (for black); originally I had only a dashed pattern [1,2,1,10], corresponding to one pixel shown, two not, one shown, 10 not, which is a dot-dot-space pattern. Then I commented that out, setting the dash to (None,None), a very formal way of saying solid line, and added the marker 'o', for circle.

我还设置了一个常量" MARKERSIZE,它将设置每个标记的大小,因为我发现默认大小会有点大.

I also set a 'constant' MARKERSIZE, which will set the size of each marker, because I found the default size to be a little large.

当行中已经有破折号或标记模式时,这显然不能解决问题,但是您可以使用这些例程作为构建更复杂转换器的起点.例如,如果原始图具有红色实线和红色虚线,则使用这些例程,它们都将变成黑色的点划线.使用它们时要记住一些事情.

This obviously does not handle the case when your lines already have a dash or marker patter, but you can use these routines as a starting point to build a more sophisticated converter. For example if you original plot had a red solid line and a red dotted line, they both would turn into black dash-dot lines with these routines. Something to keep in mind when you use them.

这篇关于matplotlib-黑色&白色颜色图(带有破折号,点等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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