[英]How to create a grouped bar plot from lists of uneven length
I am trying to plot groups of data which have different length of data.我正在尝试 plot 具有不同数据长度的数据组。 Do you have any idea how I can visualize a female list containing only two objects without filling up the rest of them with zeros to get the length of the male list?你知道我如何可视化一个只包含两个对象的女性列表而不用零填充它们的 rest 以获得男性列表的长度吗?
This is the code, which I got so far:这是我到目前为止得到的代码:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1', 'G2', 'G3', 'G4']
male = [1, 3, 10, 20]
female = [2, 7]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, male, width, label='male')
rects2 = ax.bar(x + width/2, female, width, label='female')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
fig.tight_layout()
plt.show()
You can make two different array for the x-positions:您可以为 x 位置创建两个不同的数组:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1', 'G2', 'G3', 'G4']
male = [1, 3, 10, 20]
female = [2, 7]
x_male = np.arange(len(male))
x_female = np.arange(len(female))
offset_male = np.zeros(len(male))
offset_female = np.zeros(len(female))
shorter = min(len(x_male), len(x_female))
width = 0.35 # the width of the bars
offset_male[:shorter] = width/2
offset_female[:shorter] = width/2
fig, ax = plt.subplots()
rects1 = ax.bar(x_male - offset_male, male, width, label='male')
rects2 = ax.bar(x_female + offset_female, female, width, label='female')
That said, this solution only works when values are missing at the end of the shorter list.也就是说,此解决方案仅在较短列表末尾缺少值时才有效。 For values missing within the list, it would be better to use None, or np.nan, as suggested by @desert_ranger.对于列表中缺少的值,最好按照@desert_ranger 的建议使用 None 或 np.nan。
If you don't want to fill them up with zeros, you could assign NAN values to them -如果您不想用零填充它们,则可以为它们分配 NAN 值 -
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1', 'G2', 'G3', 'G4']
male = [1, 3, 10, 20]
female = [2, 7,np.nan,np.nan]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
ax.bar(x - width/2, male, width, label='male')
ax.bar(x + width/2, female, width, label='female')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
fig.tight_layout()
plt.show()
pandas.DataFrame.plot.bar
Use pandas and plot with pandas.DataFrame.plot.bar
python 3.8.11
, pandas 1.3.1
, and matplotlib 3.4.2
在python 3.8.11
、 pandas 1.3.1
和matplotlib 3.4.2
中测试itertools.zip_longest
to zip lists of unequal length, which fills the missing data with a fillvalue
.创建 dataframe 时,列数据的长度必须相同,因此使用itertools.zip_longest
到fillvalue
长度不等的列表,用一个填充值填充缺失的数据。import pandas as pd
import matplotlib.pyplot as plt
from itertools import zip_longest
# data
labels = ['G1', 'G2', 'G3', 'G4']
male = [1, 3, 10, 20]
female = [2, 7]
# zip lists together
data = zip_longest(male, female)
# create dataframe from data
df = pd.DataFrame(data, columns=['male', 'female'], index=labels)
male female
G1 1 2.0
G2 3 7.0
G3 10 NaN
G4 20 NaN
# plot
p = df.plot.bar(rot=0)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
# data
labels = ['G1', 'G2', 'G3', 'G4']
male = [1, 3, 10, 20]
female = [2, 7]
# create a dataframe from the lists
df = pd.DataFrame([male, female], columns=labels, index=['male', 'female'])
G1 G2 G3 G4
male 1 3 10.0 20.0
female 2 7 NaN NaN
# plot
p = df.plot.bar(rot=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.