Python | 使用matplotlib.pyplot创建堆叠的条

Problem statement: Write a program in python (using matplotlib.pyplot) to create a scatter plot.

问题陈述:用python编写程序(使用matplotlib.pyplot)以创建散点图。

Use of scatter plot: Scatter plots are usually used to compare two variables (three if you are plotting in 3 dimensions), looking for correlation or groups.

散点图的使用:散点图通常用于比较两个变量(如果要绘制3维,则为三个变量),以查找相关性或组。

Program:

程序:

import matplotlib.pyplot as plt
import numpy as np

N=6

y1=[3,9,11,2,6,4]

y2=[6,4,7,8,3,4]

xvalues = np.arange(N)

plt.bar(xvalues,y1,color='b', label ='Team1')
plt.bar(xvalues,y2, color='r', bottom =y1, label = 'Team2')
plt.xticks(xvalues, ('V1', 'V2', 'V3', 'V4', 'V5'))

plt.xlabel('Teams')
plt.ylabel('Scores')
plt.title('Stacked Bar Graphs')
plt.legend()

Output

输出量

Stacked bar program output in Python

Explanation:

说明:

We use the data values y2 above y1. y1 and y2 are two random variables taken for the plot. For this we use the parameter bottom which indicates which value will be at the bottom. N is also a variable to assign the no of bars existing in the plot. Also we use the arrange method of numpy to evenly space the bars and assign its values to the x values variable. Xticks functions give the label to the evenly spaced xvalues. To name the axes xlabel and ylabel functions are used and to give the title to the plot the title function is used. To show the legend the legend function is used and finally to show the plot the show function.

我们使用高于y1的数据值y2 。 y1和y2是用于该图的两个随机变量。 为此,我们使用参数bottom来指示哪个值将在底部。 N也是一个变量,用于分配图中存在的条形数。 此外,我们使用numpy的的安排方法来均匀地间隔开的酒吧,并指定其值的x值的变量。 Xticks函数将标签赋予均匀间隔的xvalue。 为了命名轴,使用了xlabel和ylabel函数,并且使用了title函数为绘图赋予标题。 为了显示图例,使用了图例功能,最后使用show函数显示了情节。

翻译自: https://www.includehelp.com/python/create-a-stacked-bar-using-matplotlib-pyplot.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值