1.绘图
2.图表:属性
线条:颜色、样式、数据点
图表额外:轴标签(x,y)、图例、标题
3.有哪些图表:
折线图、柱状图、散点图、饼状图。。。
4 matpoltlib 编程模型:
1.Figure 画布
2.Axes 图表
部分代码展示
花sin图像
x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
绘制折线、柱状、条形、饼状、散点图
import numpy as np
import pandas as pd
if __name__ == '__main__':
# 1.数据准备
df = pd.read_csv("D:\\data\data.csv",index_col="年份")
print(df.head())
#2.绘制图表 matplotlib
#准备数据 x y
x = df.index.values #年份
y = df['啤酒产量(万千升)'].values
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
#画图
#1.折线图
fig, ax = plt.subplots()
ax.plot(x,y,"r--*")
ax.set(title="啤酒产量走势",xlabel="年份",ylabel="啤酒产量(万千升") #标题 x y
plt.show()
# 2.柱状图
fig, ax = plt.subplots()
ax.bar(x, y, width=0.5, color="skyblue")
ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升") # 标题 x y
plt.show()
# 3.柱状图 - 水平柱状图
fig, ax = plt.subplots()
ax.barh(x, y, 0.5, color="skyblue")
ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升") # 标题 x y
plt.show()
#4.饼图
fig, ax = plt.subplots()
ax.pie(y, labels=x)
plt.show()
#5.散点图
fig, ax = plt.subplots()
ax.scatter(x, y,c="#ff7f0e",alpha=0.5)
ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升") # 标题 x y
plt.show()
一个画布多个图表
#1.数据准备
x = np.arange(0, 1, 0.05)
y = np.sin(2*np.pi*x)
#2.画布+多个图表
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
plt.show()
# 3.画布+多个图表+数据
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax2.plot(x,y)
plt.show()
#4.修改曲线的颜色 线条样式 显示数据点
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax2.plot(x, y,"r--o")
plt.show()
#2.
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax2.plot(x, y, color="c",linestyle="--",marker="o")
plt.show()