天天看點

python 繪圖腳本系列簡單記錄

簡單記錄平時畫圖用到的python 便捷小腳本

1. 從單個檔案輸入 繪制坐标系圖

#!/usr/bin/python
# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import sys

file_name1 = sys.argv[1]
data_title = sys.argv[2]
print(file_name1)
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'

x, y = np.loadtxt(file_name1, delimiter=' ', unpack=True)
plt.plot(x, y, '-', label='read', color='red')
plt.ticklabel_format(style='plain')

plt.xlabel('time/s')
plt.ylabel('count')
plt.title(data_title)
plt.savefig(data_title + ".png")
plt.show()      

執行 ​

​python3 draw.py input.txt save-to-name​

​ 注意:

  1. ​input.txt​

    ​中的兩列資料,中間間隔空格
  2. ​plt.plot(x, y, '-', label='read', color='red')​

    ​​ 中的​

    ​-​

    ​​辨別繪制折線圖,如果改成​

    ​*​

    ​​,則表示繪制散點圖,最後一個​

    ​color​

    ​辨別坐标軸圖形的顔色。
  3. ​savefig​

    ​ 表示儲存的檔案名,可以自己定義存儲什麼格式的檔案
  4. ​title​

    ​ 指定圖形的标題
python 繪圖腳本系列簡單記錄

2. 多個檔案的輸入 畫在一個坐标軸中

#!/usr/bin/python
# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import sys

file_name1 = sys.argv[1]
file_name2 = sys.argv[2]
data_title = sys.argv[2]
print(file_name1)
print(file_name2)
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'

x, y = np.loadtxt(file_name1, delimiter=' ', unpack=True)
x1, y1 = np.loadtxt(file_name2, delimiter=' ', unpack=True)
plt.plot(x, y, '-', label='read', color='red')
plt.plot(x1, y1, '-', label='write', color='blue')
plt.ticklabel_format(style='plain')

plt.xlabel('time/s')
plt.ylabel('count')
plt.title(data_title)
plt.legend(['read', 'write'])
plt.savefig(data_title + ".png")
plt.show()