首页 > 编程知识 正文

堆积怎么显示总数,堆积怎么显示百分比

时间:2023-05-03 10:04:42 阅读:275792 作者:3763

绘制堆积柱形图

柱形图的基础绘制参见Bar函数柱形图绘制;Barh条形图绘制
堆积图是一种组合式的图形,是将若干图形堆叠起来的统计图形

使用参数 b o t t o m bottom bottom可以使柱形图的柱子向上平移相应的高度。同理对于堆积柱形图,上面的图形只要向上平移第一个图形每根柱子的高度即可,即参数 b o t t o m bottom bottom=第一个图形的柱高数据

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as plt# 字体与负号参数配置mpl.rcParams['font.sans-serif']=['SimHei']mpl.rcParams['axes.unicode_minus']=False# 产生数据x = [1, 2, 3, 4, 5]y = [6, 10, 4, 5, 1]y1 = [2, 6, 3, 8, 5]# 绘制图形plt.bar(x,y,color='cyan',tick_label=['A', 'B', 'C', 'D', 'E'],label='1班',)plt.bar(x,y1,color='blue',bottom=y,label='2班')# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

结果展示:

堆积条形图

与堆积柱形图绘制同理,对于堆积条形图,右边的图形只要向右平移第一个图形每根条块儿的宽度即可,即参数 l e f t left left=第一个图形条块儿的宽度数据

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as plt# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据x = [1, 2, 3, 4, 5]y = [6, 10, 4, 5, 1]y1 = [2, 6, 3, 8, 5]# 绘制图形plt.barh(x, y, color='cyan', tick_label=['A', 'B', 'C', 'D', 'E'], label='1班', )plt.barh(x, y1, color='blue', left=y, label='2班')# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

结果展示:

多数据并列柱形图 # 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据x = np.arange(5)y = [6, 10, 4, 5, 1]y1 = [2, 6, 3, 8, 5]bar_width=0.35# 绘制图形plt.bar(x, y, width=0.35, color='cyan', tick_label=['A', 'B', 'C', 'D', 'E'], label='1班', )plt.bar(x+bar_width, y1, width=0.35, color='blue', label='2班')# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

2班横轴坐标位置为1班横轴坐标位置+柱子宽度,这是2班每根柱子的中心位置;x轴刻度显示在1班柱体中间位置

想要x轴刻度标签放在两个柱子的中间位置,使用参数 x t i c k ( ) xtick() xtick()设置x轴标签位置

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据x = np.arange(5)y = [6, 10, 4, 5, 1]y1 = [2, 6, 3, 8, 5]bar_width=0.35tick_labels=['A', 'B', 'C', 'D', 'E']# 绘制图形plt.bar(x, y, width=0.35, color='cyan', tick_label=tick_labels, label='1班', )plt.bar(x+bar_width, y1, width=0.35, color='blue', label='2班')# 设置刻度标签位置plt.xticks(x+bar_width/2,tick_labels)# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

图形展示

绘制平行条形图 # 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据x = np.arange(5)y = [6, 10, 4, 5, 1]y1 = [2, 6, 3, 8, 5]bar_width = 0.35tick_labels = ['A', 'B', 'C', 'D', 'E']# 绘制图形plt.barh(x, y, height=0.35, color='cyan', tick_label=tick_labels, label='1班', hatch='/')plt.barh(x + bar_width, y1, height=0.35, color='blue', label='2班', hatch='//')# 设置y轴刻度标签位置plt.yticks(x+bar_width/2, tick_labels)# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

图形展示:
图中可以看出两个条形图的填充样式虽然都是 ′ / ′ '/' ′/′,但是填充密度不同。参数 h a t c h hatch hatch除了可以指定填充样式之外,还可以调整填充密度。符号字符串的符号数量越多,几何图形的密集程度越高。例如:参数 h a t c h hatch hatch可以取值 ′ / ′ '/' ′/′,则 h a t c h = ′ / / / ′ hatch='///' hatch=′///′, h a t c h = ′ / / ′ hatch='//' hatch=′//′, h a t c h = ′ / ′ hatch='/' hatch=′/′填充密度依次降低。

绘制堆积直方图

绘制堆积直方图只需要向函数hist中添加关键字参数 s t a c k e d = T r u e stacked=True stacked=True

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据scoreT1 = np.random.randint(0, 100, 100)scoreT2 = np.random.randint(0, 100, 100)bins = range(0, 101, 10)# 绘制图形plt.hist((scoreT1,scoreT2), bins=bins,stacked=True, edgecolor='k', histtype='bar', label=('1班','2班'))# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

图形展示:

改变参数 h i s t t y p e histtype histtype,设置直方图类型

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据scoreT1 = np.random.randint(0, 100, 100)scoreT2 = np.random.randint(0, 100, 100)bins = range(0, 101, 10)# 绘制图形plt.hist((scoreT1,scoreT2), bins=bins,stacked=True, edgecolor='k', histtype='stepfilled', label=('1班','2班'))# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()


当参数 s t a c k e d = F a l s e stacked=False stacked=False(默认值为False),可以绘制并排放置的直方图

# 导入第三方数据库import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# 字体与负号参数配置mpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False# 产生数据scoreT1 = np.random.randint(0, 100, 100)scoreT2 = np.random.randint(0, 100, 100)bins = range(0, 101, 10)# 绘制图形plt.hist((scoreT1,scoreT2), bins=bins,stacked=False, edgecolor='k', histtype='bar', label=('1班','2班'))# 设置x,y轴标签plt.xlabel('测试难度')plt.ylabel('试卷份数')# 显示图例plt.legend()# 展示图形plt.show()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。