我可以在 matplotlib 中循环显示线条样式吗 [英] Can i cycle through line styles in matplotlib

查看:33
本文介绍了我可以在 matplotlib 中循环显示线条样式吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在 matplotlib 中循环显示颜色列表.但是是否可以对线条样式(普通、虚线、虚线等)做类似的事情?我需要这样做,以便我的图表在打印时更容易阅读.任何建议如何做到这一点?

I know how to cycle through a list of colors in matplotlib. But is it possible to do something similar with line styles (plain, dotted, dashed, etc.)? I'd need to do that so my graphs would be easier to read when printed. Any suggestions how to do that?

推荐答案

这样的事情可能会奏效:

Something like this might do the trick:

import matplotlib.pyplot as plt
from itertools import cycle
lines = ["-","--","-.",":"]
linecycler = cycle(lines)
plt.figure()
for i in range(10):
    x = range(i,i+10)
    plt.plot(range(10),x,next(linecycler))
plt.show()

结果:

编辑更新版本 (v2.22)

import matplotlib.pyplot as plt
from cycler import cycler
#
plt.figure()
for i in range(5):
    x = range(i,i+5)
    linestyle_cycler = cycler('linestyle',['-','--',':','-.'])
    plt.rc('axes', prop_cycle=linestyle_cycler)
    plt.plot(range(5),x)
    plt.legend(['first','second','third','fourth','fifth'], loc='upper left', fancybox=True, shadow=True)
plt.show()

有关更多详细信息,请参阅 matplotlib 教程使用循环仪进行造型"
要查看输出,请单击显示图"

For more detailed information consult the matplotlib tutorial on "Styling with cycler"
To see the output click "show figure"

这篇关于我可以在 matplotlib 中循环显示线条样式吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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