简体   繁体   English

如何减少 python plot 中不同条形组之间的空间

[英]how to reduce space between different groups of bars in python plot

I am trying to plot group bar chart.. i plot this graph using the code given below Now what i want is to reduce space between group bars so the graph will be compressed我正在尝试 plot 组条形图.. i plot 这个图使用下面给出的代码现在我想要的是减少组条之间的空间,所以图表将被压缩

libraries图书馆

import numpy as np
import matplotlib.pyplot as plt

# set width of bars
barWidth = 0.1
 
# set heights of bars
a = [3,4, 15, 10, 12]
b = [2,13,4,19,1]
c = [12,3,7,8,4]
d = [12, 13,4,7,14]

 
# Set position of bar on X axis
r1 = np.arange(len(a))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
r4 = [x + barWidth for x in r3]
 
# Make the plot
plt.bar(r1, a,  width=barWidth, edgecolor='white',label='a')
plt.bar(r2, b, width=barWidth, edgecolor='white', label='b') 
plt.bar(r3, c, width=barWidth, edgecolor='white', label='c') 
plt.bar(r4, d, width=barWidth, edgecolor='white', label='d') 
#plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')
 

plt.xticks([r + barWidth for r in range(len(a))], ['A','B', 'C', 'D', 'E'])

plt.yticks(np.arange(0, 20+1, 4))
# Create legend & Show graphic
plt.legend()
plt.show()

在此处输入图像描述

Just Use a Pandas dataframe.只需使用 Pandas dataframe。 It will make your code more concise and will automatically take care of these issues for you.它将使您的代码更简洁,并会自动为您处理这些问题。

import pandas as pd
import matplotlib.pyplot as plt

a = [3,4, 15, 10, 12]
b = [2,13,4,19,1]
c = [12,3,7,8,4]
d = [12, 13,4,7,14]
df = pd.DataFrame([a,b,c,d], columns=["A", "B", "C", "D", "E"]).transpose()

df.plot(kind = "bar")
plt.legend(["A", "B", "C", "D", "E"])
plt.show()

在此处输入图像描述

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

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