简体   繁体   English

如何 plot 使用 Matplotlib 在条形图中为重复值分隔条形?

[英]How to plot separate bar in bar graph for repeated values using Matplotlib?

Given a dataset:给定一个数据集:

x = [3, 2, 4, 6, 7]
y = ['a','a','b','b','c']

Here variables a and b are repeated twice.这里变量ab重复了两次。 My requirement is to plot the bar graph for each variable and for variables a and b, we need a separate bar for each a and b.我的要求是 plot 每个变量和变量 a 和 b 的条形图,我们需要为每个 a 和 b 单独的条形图。

I was trying to plot a horizontal bar graph using the code:我正在尝试使用代码 plot 水平条形图:

plt.barh(y, x)

数据集的水平条形图

Here the value of a and b are stacked and plotted in a single bar.这里ab的值被堆叠并绘制在一个条形图中。 Please help with this.请帮忙解决这个问题。

You can plot on a range and change the tick labels:您可以在范围内输入 plot 并更改刻度标签:

x = [3, 2, 4, 6, 7]
y = ['a','a','b','b','c']

import matplotlib.pyplot as plt
plt.barh(range(len(x)), x)
plt.yticks(range(len(x)), y)

output: output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM