如何在matplotlib中绘制清晰可读的灰度线图?

3 投票
1 回答
1612 浏览
提问于 2025-04-17 21:15

我是一名计算机科学的本科生。在大多数课程中,我需要制作一些图表来展示我的结果。我的教授们大多数希望这些图表能够打印出来(或者如果他们接受PDF格式的话,他们会自己打印一份)以便评分。我使用matplotlib和其他一些工具来生成这些图表,这样做效果不错。

不过,我遇到的问题是,我打印出来的(彩色)折线图往往难以辨认。比如说,这里有一个相对简单的例子:

enter image description here

还有一个更糟糕的例子(可能是我设计图表本身的问题)是:

enter image description here

当这些图表在黑白打印机上打印出来时,各条线就变得完全无法区分了。

下面是我可能用来生成图表的一个脚本示例。我真的很想找到一种方法,让图表在黑白打印时看起来尽可能清晰——我该怎么做呢?有哪些有效的技巧可以提高黑白图表的可读性呢?

from matplotlib import pyplot

SERIES_COLORS = 'bgrcmyk'

def plot_series(xy_pairs, series, color):
    label_fmt = "{}Lock"
    x, y_lists = zip(*xy_pairs)
    normalized_ys = [[y / numpy.linalg.norm(ys) for y in ys]
                     for ys in y_lists]

    y = [numpy.average(y_list) for i, y_list
         in enumerate(normalized_ys)]
    y_err = [numpy.std(y_list) for i, y_list in
             enumerate(normalized_ys)]

    pyplot.errorbar(x, y, y_err,
                    label=label_fmt.format(series),
                    fmt='{}o-'.format(color)
                    ls='-')


def main():
    big_dataset = {
        'a': data_for_a,
        'b': data_for_b,
        'c': data_for_c,.
        ....
    }

    for series, color in zip(SERIES_COLORS, big_dataset):
        processed = do_work(big_dataset[series])

        plot_series(processed, series, color)

    pyplot.show()

1 个回答

3

你可以尝试不同的线条样式和标记。

这是一个很好的例子,来自于 http://matplotlib.org/examples/pylab_examples/line_styles.html

#!/usr/bin/env python
# This should probably be replaced with a demo that shows all
# line and marker types in a single panel, with labels.

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np

t = np.arange(0.0, 1.0, 0.1)
s = np.sin(2*np.pi*t)
linestyles = ['_', '-', '--', ':']
markers = []
for m in Line2D.markers:
    try:
        if len(m) == 1 and m != ' ':
            markers.append(m)
    except TypeError:
        pass

styles = markers + [
    r'$\lambda$',
    r'$\bowtie$',
    r'$\circlearrowleft$',
    r'$\clubsuit$',
    r'$\checkmark$']

colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')

plt.figure(figsize=(8,8))

axisNum = 0
for row in range(6):
    for col in range(5):
        axisNum += 1
        ax = plt.subplot(6, 5, axisNum)
        color = colors[axisNum % len(colors)]
        if axisNum < len(linestyles):
            plt.plot(t, s, linestyles[axisNum], color=color, markersize=10)
        else:
            style = styles[(axisNum - len(linestyles)) % len(styles)]
            plt.plot(t, s, linestyle='None', marker=style, color=color, markersize=10)
        ax.set_yticklabels([])
        ax.set_xticklabels([])

plt.show()

你还可以把它们全部结合在一起使用。

x = linspace(0,1,10)
ls = ["-","--","-."]
markers = ["o","s","d"]
clrs = ["k"]
k = 1

for l in ls:
    for m in markers:
        for c in clrs:
            plot(x,x**k,m+l+c)
            k+=1

希望这对你有帮助。

撰写回答