Matplotlib画图之调整字体大小

Matplotlib画图之调整字体大小

在我们处理数据的时候,需要对大量的数据进行绘图,就免不了要使用到Matplotlib。而在画图进行一些细节的设置的时候,需要涉及到字体颜色大小、坐标标注等进行处理,这里我们将对一组数据进行处理。

下面是代码解释如何读取csv数据,设置刻度、图例和坐标标签字体大小,绘制图像。

#coding:utf-8
import pandas as pd
import matplotlib.pyplot as plt

#读取csv数据
data = pd.read_csv("weather.csv")

#进行列数据处理
data.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]

# fig = plt.figure()

#设置标题标注和字体大小
plt.rcParams.update({"font.size":20})#此处必须添加此句代码方可改变标题字体大小
x=plt.title("validation_acc",fontsize=20)

#设置坐标标签标注和字体大小
plt.xlabel("step",fontsize=20)
plt.ylabel("rate",fontsize=20)

#设置坐标刻度字体大小
plt.xticks(fontsize=20,rotation=90)
plt.yticks(fontsize=20)

#对数据进行绘图
plt.plot(data["天数"],data["PM2.5"])

plt.show()

绘制图像如下所示:
Matplotlib画图之调整字体大小_第1张图片
下面要进行图例显示、设置颜色以及线条格式,以及设置曲线。显示如下图所示:
Matplotlib画图之调整字体大小_第2张图片
需要绘制多组数据的曲线并进行对比,绘制图像程序如下所示:

#coding:utf-8
import pandas as pd
import matplotlib.pyplot as plt

#读取csv数据
data1 = pd.read_csv("beijing.csv")
data2 = pd.read_csv("shanghai.csv")
data3 = pd.read_csv("guangzhou.csv")
data4 = pd.read_csv("shenzhen.csv")
data5 = pd.read_csv("tianjin.csv")

#进行列数据处理
data1.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data2.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data3.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data4.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data5.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]

# fig = plt.figure()

#设置标题标注和字体大小
plt.rcParams.update({"font.size":20})
x=plt.title("validation_acc",fontsize=20)

#设置坐标标签标注和字体大小
plt.xlabel("step",fontsize=20)
plt.ylabel("rate",fontsize=20)

#设置坐标刻度字体大小
plt.xticks(fontsize=20,rotation=90)
plt.yticks(fontsize=20)

#对数据进行绘图
plt.plot(data1["天数"],data1["PM2.5"],c="yellowgreen",label="北京",linestyle="-")
plt.plot(data2["天数"],data2["PM2.5"],c="red",label="上海",linestyle="-")
plt.plot(data3["天数"],data3["PM2.5"],c="blue",label="广州",linestyle="-")
plt.plot(data4["天数"],data4["PM2.5"],c="black",label="深圳",linestyle="-")
plt.plot(data5["天数"],data5["PM2.5"],c="pink",label="天津",linestyle="-")

#设置图例字体大小和样式
plt.legend(loc="upper right",fontsize=20)
plt.show()

绘制图像如下图所示,使用不同颜色进行表示:
Matplotlib画图之调整字体大小_第3张图片我们在绘制上面图像时,发现在中文标注显示的时候会出现乱码,变成□,因此下面我们需要对格式进行处理:

#coding:utf-8
import pandas as pd
import matplotlib.pyplot as plt
# from matplotlib.font_manager import FontProperties #字体管理器
import pylab as mpl

#设置汉字格式
# font = FontProperties(fname=r"D:/biancheng/pythonCODE/Tutle/Data_Tools/font/dodo.ttf",size=20)

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
#读取csv数据
data1 = pd.read_csv("beijing.csv")
data2 = pd.read_csv("shanghai.csv")
data3 = pd.read_csv("guangzhou.csv")
data4 = pd.read_csv("shenzhen.csv")
data5 = pd.read_csv("tianjin.csv")

#进行列数据处理
data1.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data2.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data3.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data4.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]
data5.columns = ["天数","AQI","范围","质量等级","PM2.5","PM10","SO2","CO","NO","O3"]

# fig = plt.figure()

#设置标题标注和字体大小
plt.rcParams.update({"font.size":20})
x=plt.title("北上广深天五城市天气质量变化图",fontsize=20)

#设置坐标标签标注和字体大小
plt.xlabel("月份",fontsize=20)
plt.ylabel("PM2.5",fontsize=20)

#设置坐标刻度字体大小
plt.xticks(fontsize=20,rotation=90)
plt.yticks(fontsize=20)

#对数据进行绘图
plt.plot(data1["天数"],data1["PM2.5"],c="yellowgreen",label="北京",linestyle="-")
plt.plot(data2["天数"],data2["PM2.5"],c="red",label="上海",linestyle="-")
plt.plot(data3["天数"],data3["PM2.5"],c="blue",label="广州",linestyle="-")
plt.plot(data4["天数"],data4["PM2.5"],c="black",label="深圳",linestyle="-")
plt.plot(data5["天数"],data5["PM2.5"],c="pink",label="天津",linestyle="-")

#设置图例字体大小和样式
plt.legend(loc="upper right",fontsize=20)
plt.show()

绘制图像如下图所示:Matplotlib画图之调整字体大小_第4张图片附录:本文绘图所使用数据来自https://www.aqistudy.cn/historydata/
本文制成csv数据如下:https://download.csdn.net/download/qq_36789311/11501130

本次学习教程如上所示,欢迎大家关注、批评指正。

你可能感兴趣的:(数据处理,数据处理,matplotlib)