坐标轴的修改
使用下表所示的数据进行测试。将下表数据复制粘贴到txt中,保存为example.txt
年份 PM2.5 PM10 SO2 NO2 CO-95per
2015/1/1 72 114 19 48 1.5
2015/2/1 73 97 14 39 1.6
2015/3/1 40 59 10 32 1.3
2015/4/1 44 84 13 33 1.2
2015/5/1 28 58 10 27 1
2015/6/1 20 44 9 22 0.8
2015/7/1 30 56 11 27 1
2015/8/1 34 68 13 29 1
2015/9/1 29 52 11 28 1.1
2015/10/1 53 105 18 41 1.3
2015/11/1 34 60 13 34 1.3
2015/12/1 41 66 15 38 1.2
2016/1/1 43 69 13 40 1.6
使用pandas进行读取
读取txt文件,在pandas中使用语句pd.read_table()
import pandas as pd
filename = r'...\\example.txt' # 设置文件存储的绝对或相对路径
data = pd.read_table(filename, encoding="gbk")
绘制PM2.5随年份的变化情况,从下图来看x轴的标签并不是以时间类型进行表示,而且x轴上的标签过于密集妨碍观察图像。
import matplotlib.pyplot as plt
plt.plot(data.loc[:,"年份"], data.loc[:, "PM2.5"])
plt.show()
就上面出现的问题,需要将x轴的数据转化为时间类型;把x轴上的标签布置的更容易观察。
将x轴的数据转换为时间类型:
把x轴上的标签进行旋转:
# (1)自动旋转日期标记
plt.gcf().autofmt_xdate()
#(2)调整旋转的角度
plt.xticks(rotation=60)
设置坐标轴的主题
plt.title("主题")
plt.xlabel("年份")
plt.ylabel("PM2.5")