我可以在matplotlib中循环浏览线型吗 [英] Can i cycle through line styles in matplotlib

查看:92
本文介绍了我可以在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天全站免登陆