python应用-matplotlib绘图

时间:2022-12-21 10:30:01

详细内容参看:官网


测试数据:

# 传入的参数
x=[1,2,3,4,5]
squares=[1,4,9,16,25]  

简单的绘图

import matplotlib.pyplot as plt

def plot1(x,squares):    
    """plot simple pic"""
       
    plt.plot(x,squares,linewidth=1)
    # or plt.plot(squares)
    
    plt.show()
  • plt.plot(x,y)
  • plt.show()

改变文字、字体、线宽

def plot2(x,squares):
    """change literature and line width"""
    plt.plot(x,squares,linewidth=2)
    
    plt.title("Literatures",fontsize=24)
    plt.xlabel("xlabel",fontsize=14)
    plt.ylabel("ylabel",fontsize=14)
    
    # set labelsize of x, y
    plt.tick_params(axis='both',labelsize=14)
    
    plt.show()
  • plt.title("title")
  • plt.xlabel('xlabel')
  • plt.ylabel('ylabel')
  • plt.tick_params(axis,labelsize)

自动生成数据

def plot3():
    """automatically compute the data"""
    x_val = list(range(1,1001))
    y_val = [x**2 for x in x_val]
    
    plt.scatter(x_val,y_val,s=5)

    plt.title("Literatures",fontsize=24)
    plt.xlabel("xlabel",fontsize=14)
    plt.ylabel("ylabel",fontsize=14)

    # set labelsize of x, y
    plt.tick_params(axis='both',labelsize=14)

    plt.axis([0,1100,0,1100000])

    plt.show()   
  • plt.axis([0,1100,0,110000])
  • plt.scatter(x,y,s)

颜色改变(很漂亮)

python应用-matplotlib绘图

def plot4():
    """change the color"""
    x_val = list(range(1,1001))
    y_val = [x**2 for x in x_val]
    
    # delete the default edgecolor(black)
    # change color by adjusting c
    plt.scatter(x_val,y_val,edgecolor='none',c=(0.5,0.6,0.3),s=5)

    plt.title("Literatures",fontsize=24)
    plt.xlabel("xlabel",fontsize=14)
    plt.ylabel("ylabel",fontsize=14)

    # set labelsize of x, y
    plt.tick_params(axis='both',labelsize=14)
    
    plt.axis([0,1100,0,1100000])
    
    plt.show()  
  • plt.scatter(x,y,edgecolor='none',c,s)
    • c用来调节颜色
    • s代表size
def plot5():
    """using colormap"""
    x_val = list(range(1,1001))
    y_val = [x**2 for x in x_val]
    
    # delete the default edgecolor(black)
    # using cmap to make gradient
    plt.scatter(x_val,y_val,edgecolor='none',c=y_val,s=5,cmap=plt.cm.Blues)

    plt.title("Literatures",fontsize=24)
    plt.xlabel("xlabel",fontsize=14)
    plt.ylabel("ylabel",fontsize=14)

    # set labelsize of x, y
    plt.tick_params(axis='both',labelsize=14)
    
    plt.axis([0,1100,0,1100000])
    
    plt.show()
  • plt.scatter(x,y,edgecolor,c,s,cmap)
    • cmap颜色映射
      • plt.cm.Blues
      • plt.cm.Reds
      • plt.cm.Greens
      • plt.cm.Paired
      • plt.cm.binary
      • plt.cm.seismic

保存图片

def plot6():
    """using colormap"""
    x_val = list(range(1,1001))
    y_val = [x**2 for x in x_val]
    
    # delete the default edgecolor(black)
    # using cmap to make gradient
    # cmap(plt.cm.*) Blues Reds Greens Paired binary seismic
    plt.scatter(x_val,y_val,edgecolor='none',c=y_val,s=5,cmap=plt.cm.seismic)

    plt.title("Literatures",fontsize=24)
    plt.xlabel("xlabel",fontsize=14)
    plt.ylabel("ylabel",fontsize=14)

    # set labelsize of x, y
    plt.tick_params(axis='both',labelsize=14)
    
    plt.axis([0,1100,0,1100000])
    
    # do not show, save it
    plt.savefig('save.png',bbox_inches='tight')
  • plt.savefig('name.jpg',bbox_inches)
    • 一般bbox_inches='tight'比较好