龙空技术网

7使用Python制作简单的图表并设置图表元素

均衡永没 190

前言:

目前同学们对“python在柱形图上加折线图”大致比较着重,咱们都想要分析一些“python在柱形图上加折线图”的相关内容。那么小编在网上网罗了一些对于“python在柱形图上加折线图””的相关知识,希望各位老铁们能喜欢,大家快快来了解一下吧!

# 案例01 在Python中制作简单的图表import matplotlib.pyplot as plt  # 导入Matplotlib模块x = [1, 2, 3, 4, 5, 6]  # 给出x坐标的数据y = [2, 4, 6, 8, 10, 12]  # 给出y坐标的数据plt.plot(x, y, color = 'red', linewidth = 3, linestyle = 'solid')  # 绘制折线图plt.show()  # 显示绘制的图表

In [2]:

# 案例01-1 在Python中制作柱形图import matplotlib.pyplot as plt  x = [1, 2, 3, 4, 5, 6]y = [50, 60, 80, 78, 95, 70]plt.bar(x, y, width = 0.8, align = 'center', color = 'blue')  #绘制柱形图plt.show()

In [3]:

# 案例01-2 在Python中制作柱形图import matplotlib.pyplot as pltx = [1, 2, 3, 4]y = [60, 25, 78, 50]plt.barh(x, y, align = 'center', color = 'blue')  plt.show()

In [4]:

# 案例01-3 在Python中制作柱形图import matplotlib.pyplot as plt  x = [25, 45, 69, 30, 80, 12]plt.pie(x)  plt.show()

In [5]:

# 案例02 在Python中导入Excel数据制作简单的图表import pandas as pd  # 导入pandas模块import matplotlib.pyplot as plt  # 导入Matplotlib模块import xlwings as xw  # 导入xlwings模块df = pd.read_excel('E:\\Python案例操作2\\销售业绩表.xlsx')  # 从指定工作簿中读取数据figure = plt.figure()  # 创建一个绘图窗口plt.rcParams['font.sans-serif'] = ['SimHei']  # 为图表中的中文文本设置默认字体,以避免中文显示乱码的问题plt.rcParams['axes.unicode_minus'] = False  # 解决坐标值为负数时无法正常显示负号的问题x = df['月份']  # 指定“月份”列为x坐标的值y = df['销售额']  # 指定“销售额”列为y坐标的值plt.bar(x, y, color = 'black')  # 制作柱形图app = xw.App(visible = False)  # 启动Excel程序workbook = app.books.open('E:\\Python案例操作2\\销售业绩表.xlsx')  # 打开要插入图表的工作簿worksheet = workbook.sheets['销售业绩']  # 选中要插入图表的工作表worksheet.pictures.add(figure, left = 500)  # 在工作表中插入柱形图workbook.save()  # 保存工作簿workbook.close()  # 关闭工作簿app.quit()  # 退出Excel程序

In [6]:

# 案例02-1 导入数据制作散点图import pandas as pdimport matplotlib.pyplot as pltimport xlwings as xwdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表.xlsx')figure = plt.figure()plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex = df['月份']y = df['销售额']plt.scatter(x, y, s = 500, color = 'red', marker = '*')app = xw.App(visible = False)workbook = app.books.open('E:\\Python案例操作2\\销售业绩表.xlsx')worksheet = workbook.sheets['销售业绩']worksheet.pictures.add(figure, left = 500)workbook.save()workbook.close()app.quit()

In [7]:

# 案例02-2 导入数据制作面积图import pandas as pdimport matplotlib.pyplot as pltimport xlwings as xwdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表.xlsx')figure = plt.figure()plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex = df['月份']y = df['销售额']plt.stackplot(x, y, color = 'red')app = xw.App(visible = False)workbook = app.books.open('E:\\Python案例操作2\\销售业绩表.xlsx')worksheet = workbook.sheets['销售业绩']worksheet.pictures.add(figure, left = 500)workbook.save()workbook.close()app.quit()

In [9]:

# 案例03 在Python中制作组合图表import pandas as pd  # 导入pandas模块import matplotlib.pyplot as plt  # 导入Matplotlib模块df = pd.read_excel('E:\\Python案例操作2\\销售业绩表1.xlsx')  # 从指定工作簿中读取数据plt.rcParams['font.sans-serif'] = ['SimHei']  # 为图表中的中文文本设置默认字体,以避免中文显示乱码的问题plt.rcParams['axes.unicode_minus'] = False  # 解决坐标值为负数时无法正常显示负号的问题x = df['月份']  # 指定数据中的“月份”列为x坐标的值y1 = df['销售额']  # 指定数据中的“销售额”列为y坐标的第1组值y2 = df['利润']  # 指定数据中的“利润”列为y坐标的第2组值plt.plot(x, y1, color = 'black', linewidth = 4)  # 用x坐标和第1组y坐标制作折线图plt.bar(x, y2, color = 'blue')  # 用x坐标和第2组y坐标制作柱形图plt.show()  # 显示绘制的图表

In [10]:

# 案例03-1 制作双折线图import pandas as pdimport matplotlib.pyplot as pltdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表1.xlsx')plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex1 = df['月份']y1 = df['销售额']y2 = df['利润']plt.plot(x1, y1, color = 'red', linewidth = 3, linestyle = 'solid')plt.plot(x1, y2, color = 'black', linewidth = 3, linestyle = 'solid')plt.show()

In [11]:

# 案例04 添加并设置图表标题和坐标轴标题import pandas as pd  # 导入pandas模块import matplotlib.pyplot as plt  # 导入Matplotlib模块df = pd.read_excel('E:\\Python案例操作2\\销售业绩表2.xlsx')  # 从指定工作簿中读取数据plt.rcParams['font.sans-serif'] = ['SimHei']  # 为图表中的中文文本设置默认字体,以避免中文显示乱码的问题plt.rcParams['axes.unicode_minus'] = False  # 解决坐标值为负数时无法正常显示负号的问题x = df['月份']  # 指定数据中的“月份”列为x坐标的值y = df['销售额']  # 指定数据中的“销售额”列为y坐标的值plt.bar(x, y, color = 'black')  # 制作柱形图plt.title(label = '各月销售额对比图',           fontdict = {'family' : 'KaiTi', 'color' : 'red', 'size' : 30},          loc = 'left')  # 添加并设置图表标题plt.xlabel('月份',            fontdict = {'family' : 'SimSun', 'color' : 'black', 'size' : 20},            labelpad = 20)  # 添加并设置x轴标题plt.ylabel('销售额',            fontdict = {'family' : 'SimSun', 'color' : 'black', 'size' : 20},            labelpad = 20)  # 添加并设置y轴标题plt.show()  # 显示绘制的图表

In [12]:

# 案例04 添加图例import pandas as pdimport matplotlib.pyplot as pltdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表2.xlsx')plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex = df['月份']y = df['销售额']plt.bar(x, y, color = 'red', label = '销售额')plt.legend(loc = 'upper left', fontsize = 20)plt.show()

In [13]:

# 案例05 添加并设置数据标签import pandas as pd  # 导入pandas模块import matplotlib.pyplot as plt  # 导入Matplotlib模块df = pd.read_excel('E:\\Python案例操作2\\销售业绩表2.xlsx')  # 从指定工作簿中读取数据plt.rcParams['font.sans-serif'] = ['SimHei']  # 为图表中的中文文本设置默认字体,以避免中文显示乱码的问题plt.rcParams['axes.unicode_minus'] = False  # 解决坐标值为负数时无法正常显示负号的问题x = df['月份']  # 指定数据中的“月份”列为x坐标的值y = df['销售额']  # 指定数据中的“销售额”列为y坐标的值plt.plot(x, y, color = 'red', linewidth = 3, linestyle = 'solid')  # 制作折线图for a,b in zip(x, y):    plt.text(a, b, b, fontdict = {'family' : 'KaiTi', 'color' : 'red', 'size': 20})  # 添加并设置数据标签plt.show()  # 显示绘制的图表

In [14]:

# 案例05-1 设置y轴的取值范围import pandas as pdimport matplotlib.pyplot as pltdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表2.xlsx')plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex = df['月份']y = df['销售额']plt.plot(x, y, color = 'red', linewidth = 3, linestyle = 'solid')plt.ylim(0, 1200000)for a,b in zip(x, y):    plt.text(a, b, b, fontdict = {'family' : 'KaiTi', 'color' : 'red', 'size': 20})plt.show()

In [16]:

# 案例06 为组合图表添加并设置次坐标轴import pandas as pd  # 导入pandas模块import matplotlib.pyplot as plt  # 导入Matplotlib模块df = pd.read_excel('E:\\Python案例操作2\\销售业绩表4.xlsx')  # 从指定工作簿中读取数据plt.rcParams['font.sans-serif'] = ['SimHei']  # 为图表中的中文文本设置默认字体,以避免中文显示乱码的问题plt.rcParams['axes.unicode_minus'] = False  # 解决坐标值为负数时无法正常显示负号的问题x = df['月份']  # 指定数据中的“月份”列为x坐标的值y1 = df['销售额']  # 指定数据中的“销售额”列为y坐标的第1组值y2 = df['同比增长']  # 指定数据中的“同比增长”列为y坐标的第2组值plt.bar(x, y1, color = 'grey', label = '销售额')  # 制作柱形图plt.legend(loc = 'upper left', fontsize = 20)  # 为柱形图添加和设置图例plt.twinx()  # 为图表设置双坐标轴plt.plot(x, y2, color = 'black', linewidth = 3, label = '同比增长')  # 制作折线图plt.legend(loc = 'upper right', fontsize = 20)  # 为折线图添加和设置图例plt.show()  # 显示绘制的图表

In [17]:

# 案例06 添加并设置网格线import pandas as pdimport matplotlib.pyplot as pltdf = pd.read_excel('E:\\Python案例操作2\\销售业绩表.xlsx')plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsex = df['月份']y = df['销售额']plt.plot(x, y, color = 'black', linewidth = 3, linestyle = 'solid')plt.grid(b = True, axis = 'y', color = 'red', linestyle = 'dashed', linewidth = 1)plt.show()

链接:

提取码:lfld

标签: #python在柱形图上加折线图