matplotlib画线(2)
这篇随笔是matplotlib画线的补充
>>> #nocl参数控制图例中有几列,
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>> #nocl参数控制图例中有几列,
>>> x = np.arange(0,10,1)
>>> plt.plot(x,x,x,x*2,x,x/2)
[<matplotlib.lines.Line2D object at 0x0000000012CC21D0>, <matplotlib.lines.Line2D object at 0x0000000012CC22B0>, <matplotlib.lines.Line2D object at 0x0000000012CC2978>]
>>> plt.legend(['normal','_fast','slow'],loc=0,ncol=2)
Warning (from warnings module):
File "E:\Python\lib\site-packages\matplotlib\legend.py", line 593
'legend.'.format(handle, label))
UserWarning: The handle <matplotlib.lines.Line2D object at 0x0000000012CC22B0> has a label of '_fast' which cannot be automatically added to the legend.
<matplotlib.legend.Legend object at 0x0000000012D60390>
>>> plt.show()
>>> #修改线条样式u
>>> #linestyle,color,marker
>>> x1 = np.random.randn(1000)
>>> x2 = np.random.randn(1000)
>>> x3 = np.random.randn(1000)
>>> plt.plot(x1.cumsum(),c='red',linestyle='--' ,marker = 'o')
[<matplotlib.lines.Line2D object at 0x0000000013530748>]
>>> plt.plot(x2.cumsum(),c='green',linestyle='-.',marker='>')
[<matplotlib.lines.Line2D object at 0x0000000013530B70>]
>>> plt.plot(x2.cumsum(),c='blue',linestyle=':',marker='*')
[<matplotlib.lines.Line2D object at 0x0000000013530F28>]#设置三条线的样式
>>> plt.legend (['x1','x2','x3'])
<matplotlib.legend.Legend object at 0x0000000013521C18>
>>> plt.show()
>>> #保存图片,
>>> plt.savefig('C:a//pic.jpg')
>>> plt.savefig('C:a//pic1.png',dpi=500)
>>> #savefig的可选项,png,jpg,svg等
>>> #dpi设置图片分辨率
>>> #图片的背景色默认为白色可以通过facecolor来设置背景色
>>> #plot语句中还可以设置线的颜色可以直接指定color或c为某种颜色比如red或者用16进制‘#ffffff’,还可设置线型和点型
>>> x= np.arange(0,10,1)
>>> plt.plot(x,np.sin(x),'r')
[<matplotlib.lines.Line2D object at 0x000000002236FB00>]
>>> plt.show()
>>> #或者plt.plot(x,np.sin(x),'#ff0000')
>>> #或者plt.plot(x,np.sin(x),color=(0.1,0.8,0.2))rgb三颜色模型
>>> #jpg和png的区别,格式不同,导致存数据的时候,png是归一化的红绿蓝数据rgb,jpg为0-255
>>> #透明度alpha参数
>>> x= np.arange(0,10,1)
>>> plt.plot(x,np.sin(x),'r',alpha=0.3)#透明度为0.3,越小越透明
[<matplotlib.lines.Line2D object at 0x00000000224E4E10>]
>>> plt.show()
>>> #背景色
>>> #通过subplot()方法传入facecolor参数,来设置背景色
>>> axes= plt.subplot (facecolor='green')
>>> plt.show()
#常见线型的参数如下
![](https://images2018.cnblogs.com/blog/1363376/201807/1363376-20180731121752113-1730210174.png)
#注意dashes的参数包括空格长度和线的长度
#下面是一些marker的参数
#下面是多参数连用,同时为多个曲线设置
![](https://images2018.cnblogs.com/blog/1363376/201807/1363376-20180731122444271-1436649148.png)
#不是对画板操
#而是对线进行操作
#方式三>>> #使用setp()设置属性
>>> plt.plot(x,np.sin(x))
[<matplotlib.lines.Line2D object at 0x000000001325E978>]
>>> line=plt.plot(x,np.sin(x))
>>> plt.setp(line,linestyle='--',linewidth=3,marker='o')
[None, None, None]
>>> plt.show()
>>>
#线型的知识总结