Matplotlib 不同的虚线而不是彩色线
Matplotlib different dashed lines instead of coloured lines
我经常需要在黑白打印机上打印图表,如果我想在同一张图表上显示不同的数据集,matplotlib 使用的默认不同颜色对我没有帮助。
有没有一种方法可以更改 matplotlib 默认值以循环显示一系列不同的虚线变体(如技术出版物中常见的那样),而不是循环显示不同的彩色线条?
非常感谢对此的帮助。
Matplotlib 文档有一个很好的改变线条形状的例子:
http://matplotlib.org/users/pyplot_tutorial.html#controlling-line-properties
构建一个 returns 或从循环物质中的列表中产生值之一的函数不会太困难。
来自文档中的示例:
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
在图形上生成三种不同颜色和形状的线条。
您可以使用 itertools 模块循环线型
import matplotlib.pyplot as plt
import itertools
# put all the linestyles you want in the list below (non exhaustive here)
style=itertools.cycle(["-","--","-.",":",".","h","H"])
# assuming xseries and yseries previously created (each one is a list of lists)
for x,y in zip(xseries,yseries):
plt.plot(x,y,"b"+style.next())
plt.show()
我经常需要在黑白打印机上打印图表,如果我想在同一张图表上显示不同的数据集,matplotlib 使用的默认不同颜色对我没有帮助。
有没有一种方法可以更改 matplotlib 默认值以循环显示一系列不同的虚线变体(如技术出版物中常见的那样),而不是循环显示不同的彩色线条?
非常感谢对此的帮助。
Matplotlib 文档有一个很好的改变线条形状的例子:
http://matplotlib.org/users/pyplot_tutorial.html#controlling-line-properties
构建一个 returns 或从循环物质中的列表中产生值之一的函数不会太困难。
来自文档中的示例:
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
在图形上生成三种不同颜色和形状的线条。
您可以使用 itertools 模块循环线型
import matplotlib.pyplot as plt
import itertools
# put all the linestyles you want in the list below (non exhaustive here)
style=itertools.cycle(["-","--","-.",":",".","h","H"])
# assuming xseries and yseries previously created (each one is a list of lists)
for x,y in zip(xseries,yseries):
plt.plot(x,y,"b"+style.next())
plt.show()