折线图1.最基本的折线图2.不同的线条3.线条色彩设置4.一图多个线条5.对线条进行加工处理6.子图7.添加注释8.风格设置
折线图 1.最基本的折线图
1.用python实现
import numpy as npimport matplotlib.pyplot as plt#前面的数据为x轴的坐标,后面的是y轴坐标plt.plot([1,2,3,4,5],[1,2,3,4,5])#在一个新窗口中显示折线图plt.show()
import numpy as npimport matplotlib.pyplot as plt#前面的数据为x轴的坐标,后面的是y轴坐标plt.plot([1,2,3,4,5],[1,4,9,16,25])plt.xlabel('xlabel',fontsize = 16)plt.ylabel('ylabel',fontsize = 16)#在一个新窗口中显示折线图plt.show()
大家可以对比一下两个图与代码之间的区别
2.jupyter实现
import numpy as np#pyplot是python中画图的一个接口import matplotlib.pyplot as plt#在notebook中画图,不需要调用plt.show去显示图形%matplotlib inlineplt.plot([1,2,3,4,5],[1,2,3,4,5])
对数据的改变会影响图形以及坐标的改变,如果不规定坐标轴范围则会自动设置合适的范围
import numpy as np#pyplot是python中画图的一个接口import matplotlib.pyplot as plt#在notebook中画图,不需要调用plt.show去显示图形%matplotlib inlineplt.plot([1,2,3,4,5],[1,4,9,16,25])#添加x轴上的解释,fontsize会进行字体大小的修改plt.xlabel('xlabel',fontsize=16)#添加y轴上的解释plt.ylabel('ylabel',fontsize=16)
2.不同的线条1.python实现
import numpy as npimport matplotlib.pyplot as plt#前面的数据为x轴的坐标,后面的是y轴坐标plt.plot([1,2,3,4,5],[1,4,9,16,25],'--')plt.xlabel('xlabel',fontsize = 16)plt.ylabel('ylabel',fontsize = 16)#在一个新窗口中显示折线图plt.show()
2.jupyter实现
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlineplt.plot([1,2,3,4,5],[1,4,9,16,25],':')#添加x轴上的解释,fontsize会进行字体大小的修改plt.xlabel('xlabel',fontsize=16)#添加y轴上的解释plt.ylabel('ylabel',fontsize=16)
3.线条色彩设置1.python实现
import numpy as npimport matplotlib.pyplot as plt#前面的数据为x轴的坐标,后面的是y轴坐标#1.直接用色彩+线条就可以表示plt.plot([1,2,3,4,5],[1,4,9,16,25],'m--')plt.xlabel('xlabel',fontsize = 16)plt.ylabel('ylabel',fontsize = 16)#在一个新窗口中显示折线图plt.show()
2.jupyter实现
import numpy as npimport matplotlib.pyplot as plt%matplotlib inline#在color后面加参数plt.plot([1,2,3,4,5],[1,4,9,16,25],'-.',color='r')#添加x轴上的解释,fontsize会进行字体大小的修改plt.xlabel('xlabel',fontsize=16)#添加y轴上的解释plt.ylabel('ylabel',fontsize=16)
4.一图多个线条1.pycharm实现
import numpy as npimport matplotlib.pyplot as plthu_numpy=np.arange(0,10,0.5)plt.plot(hu_numpy,hu_numpy,'r--',hu_numpy,hu_numpy**2,'bs',hu_numpy,hu_numpy**3,'go')plt.show()
2.jupyter实现
import numpy as npimport matplotlib.pyplot as plt%matplotlib inline#类似于python中的内置函数range()hu_numpy=np.arange(0,10,0.5)plt.plot(hu_numpy,hu_numpy,'r--')plt.plot(hu_numpy,hu_numpy**2,'bs')plt.plot(hu_numpy,hu_numpy**3,'go')
5.对线条进行加工处理1.pycharm实现
第一种方式
import numpy as npimport matplotlib.pyplot as pltx=np.linspace(-10,10)y=np.sin(x)#marker设置标志点,markerfacecolor标志点的颜色,markersize标志点的大小plt.plot(x,y,linewidth=3.0,color='b',linestyle=':',marker='o',markerfacecolor='r',markersize=10)plt.show()
第二种方式:先画图再设置参数
import numpy as npimport matplotlib.pyplot as pltx=np.linspace(-10,10)y=np.sin(x)line=plt.plot(x,y)#alpha为线条的透明度plt.setp(line,color='r',linewidth=2.0,alpha=0.5)plt.show()
2.jupyter实现
第一种方式
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)#marker设置标志点,markerfacecolor标志点的颜色,markersize标志点的大小plt.plot(x,y,linewidth=3.0,color='b',linestyle=':',marker='o',markerfacecolor='r',markersize=10)
第二种方式
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)line=plt.plot(x,y)#alpha为线条的透明度plt.setp(line,color='r',linewidth=2.0,alpha=0.5)
6.子图1.pycharm
import numpy as npimport matplotlib.pyplot as pltx=np.linspace(-10,10)y=np.sin(x)#subplot为子图#211表示一会要画的图为1行2列的,最后一个1表示子图当中的第一个plt.subplot(121)plt.plot(x,y,color='r')#212表示一会要画的图为1行2列的,最后一个2表示子图当中的第二个plt.subplot(122)plt.plot(x,y,color='b')plt.show()
2.jupyter
import numpy as npimport matplotlib.pyplot as plt%matplotlib inline#subplot为子图#211表示一会要画的图为两行一列的,最后一个1表示子图当中的第一个plt.subplot(211)plt.plot(x,y,color='r')#212表示一会要画的图为两行一列的,最后一个2表示子图当中的第二个plt.subplot(212)plt.plot(x,y,color='b')
7.添加注释1.pycharm
import numpy as npimport matplotlib.pyplot as pltx=np.linspace(-10,10)y=np.sin(x)plt.plot(x,y,linewidth=3.0,color='b',linestyle=':',marker='o',markerfacecolor='r',markersize=10)plt.xlabel('x:---')plt.ylabel('y:---')#设置标题plt.title('Anan:---')#给图中的某个点加注释plt.text(0,0,'Anan')#给图中加上格子plt.grid(True)#此函数用于标注文字,xy为被注释的坐标点,xytext为注释文字的坐标位置,arrowprops为箭头参数,参数类型为字典dict,facecolor为箭头颜色,shrink为箭头大小值plt.annotate('Anan',xy=(-5,0),xytext=(-2,0.3),arrowprops=dict(facecolor='black',shrink=0.05,headlength=20,headwidth=20))plt.show()
2.jupyter
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)plt.plot(x,y,linewidth=3.0,color='b',linestyle=':',marker='o',markerfacecolor='r',markersize=10)plt.xlabel('x:---')plt.ylabel('y:---')#设置标题plt.title('Anan:---')#给图中的某个点加注释plt.text(0,0,'Anan')#给图中加上格子plt.grid(True)#此函数用于标注文字,xy为被注释的坐标点,xytext为注释文字的坐标位置,arrowprops为箭头参数,参数类型为字典dict,facecolor为箭头颜色,shrink为箭头大小值plt.annotate('Anan',xy=(-5,0),xytext=(-2,0.3),arrowprops=dict(facecolor='black',shrink=0.05,headlength=20,headwidth=20))
8.风格设置所有的风格样式
1.pycharm
原本样子
import matplotlib.pyplot as pltimport numpy as npx=np.linspace(-10,10)y=np.sin(x)plt.plot(x,y)plt.show()
import matplotlib.pyplot as pltimport numpy as npx=np.linspace(-10,10)y=np.sin(x)plt.style.use('dark_background')plt.plot(x,y)plt.show()
import matplotlib.pyplot as pltimport numpy as npx=np.linspace(-10,10)y=np.sin(x)plt.style.use(['ggplot','bmh'])plt.plot(x,y)plt.show()
import matplotlib.pyplot as pltimport numpy as npx=np.linspace(-10,10)y=np.sin(x)plt.xkcd()plt.plot(x,y)plt.show()
2.jupyter
import matplotlib.pyplot as pltimport numpy as np%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)plt.plot(x,y)
import matplotlib.pyplot as pltimport numpy as np%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)plt.style.use('dark_background')plt.plot(x,y)
不仅可以有一种风格效果,还可以两种或多种叠加
import matplotlib.pyplot as pltimport numpy as np%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)plt.style.use(['ggplot','bmh'])plt.plot(x,y)
还有一种独特的样式
import matplotlib.pyplot as pltimport numpy as np%matplotlib inlinex=np.linspace(-10,10)y=np.sin(x)plt.xkcd()plt.plot(x,y)