介绍
matplotlib是python的一个优秀的数据可视化库,
能够绘制常用的数据分析图标,还能绘制三维图形
安装
pip install matplotlib
官方文档:
https://matplotlib.org/tutorials/index.html
1.基础用法
import matplotlib.pyplot as plt
# 准备数组
import numpy as np
x = np.arange(0, 1, 0.05)
print(x)
# [0. 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65
# 0.7 0.75 0.8 0.85 0.9 0.95]
#y = sin(x*pi*x)
y = np.sin(2*np.pi*x)
print(y)
#绘制正弦图
plt.plot(x,y)
plt.show()
# 改变线条颜色:蓝色
# plt.plot(x,y,"b")
# 改变线条颜色:蓝色虚线
# plt.plot(x,y,"b--")
# 显示数据点
# plt.plot(x,y,"b--o*")
# 图标添加标题
plt.title("my fist plot")
# 坐标轴添加标签
plt.xlabel("Angle")
plt.ylabel("sin")
# 添加线条图例说明
plt.plot(x, y, "b--o", label="sin")
# 设置线条图例说明位置
plt.legend(loc='best')
plt.show()
# 绘制多个图表figure和subplot
fig = plt.figure()
ax1 = fig.add_subplot(221) # 221:表示创建2*2的图标框,第1个图表
ax2 = fig.add_subplot(222) # 第2个图标
ax3 = fig.add_subplot(223) # 第3个图标
ax4 = fig.add_subplot(224) # 第4个图标
#分别绘图
ax2.plot(x, y)
ax1.plot(x, y1)
#另一种绘制方法
# fig,ax = plt.subplots(2,2)
# 绘图
# ax[0,1].plot(x,y)
#颜色,
ax2.plot(x, y, 'r')
#线型和标记
ax1.plot(x, y1, 'b--*')
# ax1.plot(x, y, color='b', linestyle='--', marker='*')
#标题
ax2.set(title='sin')
ax1.set(title='cos')
#标签
ax1.set(xlabel="x")
ax1.set(ylabel="y")
#网格线
ax1.grid()
#一个图标绘制多个线条
ax3.plot(x,y, 'r--*', label="sin")
ax3.plot(x,y1, 'b--o', label="cos")
ax3.plot(x,x, 'g', label="test")
# 设置线条图例说明位置
ax3.legend(loc='best') #best:最合适的位置
plt.show()
#将图表保存到本地
# 方式1
# 点击图表保存按钮
# 方式2
fig.savefig("myfig.png")
plt.show()
2. 简单图表
data.csv源数据
年份,人均GDP(元),产量(万千升),人均消费指数
2000,7857.7,2231.3,100.4
2001,8521.7,2288.9,100.7
2002,9339.2,2402.7,99.2
2003,10542.0,2540.5,101.2
2004,12233.5,2958.9,103.9
2.1 折线图
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.csv", index_col="年份")
print(df.head())
# 设置坐标轴
y = df["人均GDP(元)"].values
print(y) # [ 7857.7 8521.7 9339.2 10542. 12233.5]
x = df.index.values
print(x) # [2000 2001 2002 2003 2004]
# 绘制折线图
fig , ax = plt.subplots()
ax.plot(x, y, "r-*")
ax.set(title="人均GDP", xlabel="年份", ylabel="人均GDP") #中国问显示乱码,需要设置默认字体
#指定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
#添加数据标签
for i, j in zip(x, y):
plt.text(i, j+1, '%.2f'%j, ha='center', va='bottom', fontsize=10.5)
plt.show()
2.2 柱形图
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.csv", index_col="年份")
print(df.head())
# 设置坐标轴
y = df["人均GDP(元)"].values
print(y) # [ 7857.7 8521.7 9339.2 10542. 12233.5]
x = df.index.values
print(x) # [2000 2001 2002 2003 2004]
# 绘制柱状图
fig, ax = plt.subplots()
# ax.bar(x, y, color="skyblue")
#设置柱子宽度
ax.bar(x, y,0.5, color="skyblue")
#设置标题也可用:
# ax.set_title("人均GDP")
# ax.set_xlabel="年份"
ax.set(title="人均GDP", xlabel="年份", ylabel="人均GDP") #中国问显示乱码,需要设置默认字体
#指定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['Kaiti']
# 添加数据标签
for i, j in zip(x, y):
plt.text(i, j + 1, '%.2f' % j, ha='center', va='bottom', fontsize=10.5)
plt.show()
2.3 条形图(水平柱状图)
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.csv", index_col="年份")
print(df.head())
# 设置坐标轴
y = df["人均GDP(元)"].values
print(y) # [ 7857.7 8521.7 9339.2 10542. 12233.5]
x = df.index.values
print(x) # [2000 2001 2002 2003 2004]
# 绘制条形图: barh
fig, ax = plt.subplots()
#设置柱子宽度
ax.barh(x, y,0.5, color="skyblue")
ax.set(title="人均GDP", xlabel="人均GDP", ylabel="年份") #中国问显示乱码,需要设置默认字体
#指定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['Kaiti']
plt.show()
2.4 饼状图
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.csv", index_col="年份")
print(df.head())
# 设置坐标轴
y = df["人均GDP(元)"].values
print(y) # [ 7857.7 8521.7 9339.2 10542. 12233.5]
x = df.index.values
print(x) # [2000 2001 2002 2003 2004]
# 绘制饼图: pie
fig, ax = plt.subplots()
# 设置是否分离:0表示不分离,这里是分离第5个扇形
explode = (0, 0, 0, 0, 0.1, 0)
ax.pie(y, labels=x, explode=explode, autopct='%1.2f%%')
ax.set(title="人均GDP占比") # 中国问显示乱码,需要设置默认字体
# 指定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['Kaiti']
plt.show()
2.5 散点图
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.csv", index_col="人均GDP(元)")
print(df.head())
# 设置坐标轴
y = df["人均消费指数"].values
print(y) # [100.4 100.7 99.2 101.2 103.9 80.9]
x = df.index.values
print(x) # [ 7857.7 8521.7 9339.2 10542. 12233.5 1569.5]
# 绘制散点图:
fig, ax = plt.subplots()
ax.scatter(x, y)
#绘制”人均GDP(元)“和”人均消费指数“之间的关系
ax.set(title="人均消费指数情况", xlabel="人均GDP(元)", ylabel="人均消费指数") # 中文显示乱码,需要设置默认字体
# 指定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['Kaiti']
plt.show()
2. pandas中的绘图函数
源数据
年份,人均GDP(元),产量(万千升),人均消费指数
2000,7857.7,2231.3,100.4
2001,8521.7,2288.9,100.7
2002,9339.2,2402.7,99.2
2003,10542.0,2540.5,101.2
2004,12233.5,2958.9,103.9
2005,1569.5,256.9,80.9
2.1 折线图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#折线图
df["人均GDP(元)"].plot()
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.title("人均GDP趋势图")
plt.xlabel("年份")
plt.ylabel("人均GDP")
plt.show()
2.2 柱状图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#柱状图
df["人均GDP(元)"].plot(kind="bar",title="人均GDP趋势图")
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.xlabel("年份")
plt.ylabel("人均GDP")
plt.show()
2.3 条形图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#水平柱形图
df["人均GDP(元)"].plot(kind="barh",title="人均GDP趋势图", color="green")
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.xlabel("人均GDP")
plt.ylabel("年份")
plt.show()
2.4 饼图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#饼图
df["人均GDP(元)"].plot(kind="pie",title="人均GDP趋势图")
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.show()
2.5 面积图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#面积图
df["人均GDP(元)"].plot(kind="area",title="人均GDP趋势图")
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.xlabel("年份")
plt.ylabel("人均GDP")
plt.show()
2.6 绘制所有数据
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
# df.plot(title="人均GDP趋势图") #折线图
df.plot(kind="bar", title="人均GDP趋势图") #柱状图
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.xlabel("年份")
plt.show()
2.7 堆积柱形图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("data.csv", index_col="年份")
#堆积柱形图
df.plot(kind="bar", title="人均GDP趋势图", stacked=True)
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.xlabel("年份")
plt.show()
2.8 直方图
import matplotlib.pyplot as plt
#直方图统计的是频数(在某个区间内,出现的次数)
from pandas import Series
hist_data = Series([1,2,1,2,3,1,5,5,5,6,9,8,7,1])
#定义分组
# mybins = [1,3,5,7]
# hist_data.plot(kind="hist", bins=mybins, title="频率分布图")
hist_data.plot(kind="hist", title="频率分布图")
plt.xlabel("年份")
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
plt.show()