目录
折线图制作
1、制作折线图展示数据变化趋势
2、制作折线图并为最高点添加数据标签
3、制作平滑的折线图
折线图制作
月销售表:https://download.csdn.net/download/weixin_44940488/19230071
import pandas as pd
import matplotlib.pyplot as plt
import xlwings as xw
df = pd.read_excel('月销售表.xlsx')
figure = plt.figure() # 创建一个绘制窗口
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# X轴和Y轴数据标签
x = df['月份']
y = df['销售额']
plt.plot(x, y, color = 'red', linewidth = '3', linestyle = 'solid') # 制作折线图
plt.title(label = '月销售额趋势图', fontdict = {'color' : 'black', 'size' : 30}, loc = 'center') # 添加并设置图表标题
for a,b in zip(x,y): # 遍历折线图的每一个数据点
plt.text(a, b + 0.2, (a, '%.0f' % b), ha = 'center', va = 'bottom', fontsize = 10) # 添加并设置数据标签
# plt.axis('off') # 隐藏坐标轴
# plt.show() # 直接显示图表
# 将图表保存到工作簿中
app = xw.App(visible = False) # 启动Excel程序
workbook = app.books.open('月销售表.xlsx') # 打开要插入图表的工作簿
worksheet = workbook.sheets['Sheet1'] # 选中工作表“sheet1”
worksheet.pictures.add(figure, name = '图片1', update = True, left = 200) # 在工作表中插入制作的折线图
workbook.save('折线图.xlsx') # 另存工作簿
workbook.close() # 关闭工作簿
app.quit() # 退出Excel程序
import pandas as pd
import matplotlib.pyplot as plt
import xlwings as xw
df = pd.read_excel('月销售表.xlsx')
figure = plt.figure()
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = df['月份']
y = df['销售额']
plt.plot(x, y, color = 'red', linewidth = '3', linestyle = 'solid')
plt.title(label = '月销售额趋势图',fontdict = {'color' : 'black', 'size' : 30}, loc = 'center')
max1 = df['销售额'].max() # 获取最高销售额
df_max = df[df['销售额'] == max1] # 获取最高销售额对应的行数据
# 为最高点添加数据标签
for a,b in zip(df_max['月份'], df_max['销售额']):
plt.text(a, b + 0.05, (a, '%.0f' % b), ha = 'center', va = 'bottom', fontsize = 10)
# plt.show()
# 将图表保存到工作簿中将图表保存到工作簿中
app = xw.App(visible = False)
workbook = app.books.open('月销售表.xlsx')
worksheet = workbook.sheets['Sheet1']
worksheet.pictures.add(figure, name = '图片1', update = True, left = 200)
workbook.save('显示最高点数据标签的折线图.xlsx')
workbook.close()
app.quit()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
import xlwings as xw
df = pd.read_excel('月销售表.xlsx')
figure = plt.figure()
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = df['月份']
y = df['销售额']
xnew = np.arange(1, 12, 0.1)
func = interpolate.interp1d(x, y, kind = 'cubic')
ynew = func(xnew)
plt.plot(xnew, ynew, color = 'red', linewidth = '3', linestyle = 'solid') # 绘制平滑折线图
plt.title(label = '月销售额趋势图',fontdict = {'color' : 'black', 'size' : 30}, loc = 'center')
plt.xlabel('月份', fontdict = {'family' : 'SimSun', 'color' : 'black', 'size' : 20}, labelpad = 20)
plt.ylabel('销售额', fontdict = {'family' : 'SimSun', 'color' : 'black', 'size' : 20}, labelpad = 20)
plt.xlim(0, 12) # 设置图表X的取值范围
# plt.show()
# 将图表保存到工作簿中
app = xw.App(visible = False)
workbook = app.books.open('月销售表.xlsx')
worksheet = workbook.sheets['Sheet1']
worksheet.pictures.add(figure, name = '图片1', update = True, left = 200)
workbook.save('平滑折线图.xlsx')
workbook.close()
app.quit()