![](https://img-blog.csdnimg.cn/direct/c689032f7c954383a627a9d100ebf2f7.png)
# 成绩折线图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('成绩表.xlsx')
x = df['姓名']
y1 = df['数学']
y2 = df['语文']
y3 = df['英语']
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='数学', color='r', marker='o')
plt.plot(x, y2, label='语文', color='g', marker='o', linestyle='--')
plt.plot(x, y3, label='英语', color='b', marker='o', linestyle='-.')
plt.grid(axis='y')
plt.xlabel('姓名')
plt.ylabel('分数')
plt.legend(['数学', '语文', '英语'])
plt.yticks(range(50, 150, 10))
plt.show()
![](https://img-blog.csdnimg.cn/direct/5a233b6fed7c46a38198b7cee4b6cdae.png)
# 天气预报折线图实例
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('五一气温.xlsx')
#plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(facecolor='yellow', figsize=(10, 6))
x = df['日期']
y = df['温度']
plt.xlabel('2024年五一')
plt.ylabel('室外温度')
plt.grid(axis='y')
plt.title('五一期间室外温度折线图', fontsize=18)
for a, b in zip(x, y):
plt.text(a, b, b, ha='center', va='bottom', fontsize=12, color='g')
plt.plot(x, y, marker='o', color='r', markerfacecolor='w') # markerfacecolor => mfc
plt.show()
![](https://img-blog.csdnimg.cn/direct/0290c92ae4b94a9fb716aa21abbeebb4.png)
# 基本柱状图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('产品销售统计.xlsx')
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(10, 6))
x = df['产品名称']
height = df['总量']
plt.title('产品年度销量统计图', fontsize=18)
plt.grid(axis='y', linestyle='--')
plt.xlabel('产品名称')
plt.ylabel('销量')
for a,b in zip(x,height):
plt.text(a,b,format(b,','),ha='center',va='center',fontsize=12,color='b',alpha=0.9)
plt.bar(x,height,width=0.5,alpha=0.5)
plt.legend(('销售额',))
plt.show()
![](https://img-blog.csdnimg.cn/direct/78dd1b3f863b457298bb6947fc801c89.png)
# 多柱状图 4个季度
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_excel('产品销售统计.xlsx')
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(10, 6))
x = df['产品名称']
x = np.array([0,1,2,3,4,5,6,7])
y1 = df['1月']+df['2月']+df['3月']
y2 = df['4月']+df['5月']+df['6月']
y3 = df['7月']+df['8月']+df['9月']
y4 = df['10月']+df['11月']+df['12月']
bar_width = 0.2
plt.ylabel('季度销售量')
plt.xlabel('产品名称')
plt.title('季度销量统计图')
plt.bar(x, y1, bar_width, color='c', alpha=0.5)
plt.bar(x+bar_width, y2, bar_width, color='b', alpha=0.5)
plt.bar(x+2*bar_width, y3, bar_width, color='g', alpha=0.5)
plt.bar(x+3*bar_width, y4, bar_width, color='y', alpha=0.5)
data=df['产品名称']
plt.xticks(x,data)
for a,b in zip(x,y1):
plt.text(a,b,format(b,','),ha='center',va='bottom',fontsize=6,color='r',alpha=0.9)
for a,b in zip(x,y2):
plt.text(a+bar_width,b,format(b,','),ha='center',va='bottom',fontsize=6,color='r',alpha=0.9)
for a,b in zip(x,y3):
plt.text(a+2*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=6,color='r',alpha=0.9)
for a,b in zip(x,y4):
plt.text(a+3*bar_width,b,format(b,','),ha='center',va='bottom',fontsize=6,color='r',alpha=0.9)
plt.legend(['第一季度','第二季度','第三季度','第四季度'])
plt.show()
![](https://img-blog.csdnimg.cn/direct/95a164e132d14ab0a340017a92c65781.png)
# 基础饼图
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_excel('JD手机销售数据.xlsx')
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(10, 6))
x = df['北京出库']
labels = df['商品名称']
plt.pie(x,labels=labels,autopct='%1.1f%%',labeldistance=1.02,startangle=90,textprops={'fontsize':12})
plt.axis('equal')
plt.title('JD手机品牌北京出库分析', fontsize=18)
plt.legend(df['商品名称'], loc='upper left') # frameon=False 图例去边框
plt.show()