简单记录平时画图用到的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
注意:
-
input.txt
中的两列数据,中间间隔空格 -
plt.plot(x, y, '-', label='read', color='red')
中的-
标识绘制折线图,如果改成*
,则表示绘制散点图,最后一个color
标识坐标轴图形的颜色。 -
savefig
表示保存的文件名,可以自己定义存储什么格式的文件 -
title
指定图形的标题
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()
运行:python3 draw.py iostat-string-set-read.txt iostat-string-set-write.txt iostat-string-set
将两个文件的数据展示在一个坐标轴中,并保存最中的结果。