程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] Python Matplotlib draws and saves multiple pictures

編輯:Python

1. demand

  • Draw a picture to compare the observed value with the predicted value

2. Code

  • Sample code

    import matplotlib.pyplot as plt
    import numpy as np
    obs_x_data = [20.61782455, 20.3446064, 20.33182907, 20.41394997, 20.42613411, 20.38294792, 20.29493904, 20.11700439, 19.44220352, 17.69989204,
    16.50032616, 15.84687519, 14.43702602, 12.91092873, 11.64278507, 11.01664066, 10.34288025, 9.60017872, 8.91129875, 8.14001083]
    obs_y_data = [30.85092926, 30.41317368, 30.22782707, 29.81970787, 29.28694534, 29.19682884, 29.06859398, 29.03396034, 29.01906776, 29.49583817,
    29.87446022, 30.07171631, 30.44148064, 30.78217125, 31.01222801, 31.08432961, 31.16893959, 31.26436996, 31.36372185, 31.45003319]
    pred_x_data = [20.61782455, 20.3446064, 20.33182907, 20.41394997, 20.42613411, 20.38294792, 20.29493904, 20.11700439, 19.44220352, 17.69989204,
    16.50027275, 15.39491463, 14.33294296, 13.27159023, 12.1923933, 11.0963583, 10.00212288, 8.92199039, 7.85391903, 6.7955761]
    pred_y_data = [30.85092926, 30.41317368, 30.22782707, 29.81970787, 29.28694534, 29.19682884, 29.06859398, 29.03396034, 29.01906776, 29.49583817,
    29.87576103, 30.21777916, 30.55349922, 30.88473892, 31.22133636, 31.54788208, 31.86850739, 32.18684769, 32.4966507, 32.79633331]
    plt.figure(figsize=(12, 7.5))
    plt.plot(obs_x_data, obs_y_data, 'ro', linestyle='solid', label='ground truth')
    plt.plot(pred_x_data, pred_y_data, 'bx', linestyle='dashed', label='pred value')
    plt.xlim(5, 22)
    plt.ylim(28, 34)
    x_ticks = np.arange(5, 22, 1)
    y_ticks = np.arange(28, 34, 0.5)
    plt.xticks(x_ticks)
    plt.yticks(y_ticks)
    plt.ylabel('y position', fontsize=14)
    plt.xlabel('x position', fontsize=14)
    plt.legend(loc='upper right', ncol=1, fancybox=True, shadow=True)
    plt.title("trajectory comparison", fontsize=16)
    plt.legend() # Show Legend 
    plt.grid(True) # Show gridlines 
    plt.show() # display picture 
    
  • If you want to save the picture

    ...
    plt.savefig('/your/path/test.png') # Write picture data to file 
    plt.show() # Send picture data to the user interface library for display 
    
  • If you want to draw more than one picture : For example, multiple targets and multiple tracks

    for target in range(len(targets)):
    for index in range(len(trajectories)):
    plt.figure(figsize=(12, 7.5))
    ...
    plt.plot()
    ...
    plt.show() # Pay attention to for Out of the loop ,for Cycle through multiple drawings , Show multiple pictures after the cycle ends 
    
  • If you want to set different numbers and pixels for the picture

    for target in range(len(targets)):
    for index in range(len(trajectories)):
    plt.figure(figsize=(12, 7.5))
    ...
    plt.plot()
    ...
    # Set different picture numbers and pixels 
    plt.savefig('your/path/test_{}.png'.format(index), dpi = 200)
    plt.show()
    

    matplotlib The default pixel value is 100, That is, if output 8 x 6 A graph of spatial units , Then we can provide 8*100 x 6*100 Pixel picture file

3. Example


Reference article :
matplotlib visualization
matplotlib Store multiple pictures
savefig() Function parameter usage

created by shuaixio, 2022.06.19


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved