如何在Python或Matlab中创建叠加气泡图?

问题描述 投票:0回答:2

我正在尝试在python中创建气泡图。我的数据集看起来像这样。

Year   (Total students)  (Number Passed)
-----------------------------------------
2011       (500)              (250)
2012       (350 )            ( 150)
2013       (348 )             (100)

基本上,我想做的是创建一个气泡图,将每年通过的学生人数与学生总数相叠加。这些值将是气泡图的大小。如何使用python或matlab实现此目标?

python matplotlib seaborn matlab-figure bubble-chart
2个回答
1
投票

假设您的数据在DataFrame中,则可以执行以下操作:

from matplotlib import pyplot as plt
s = len(df.index)
plt.scatter(df["Year"], np.ones(s), s=df["Total Students"]*20, alpha=0.6)
plt.scatter(df["Year"], np.ones(s), s=df["Number Passed"]*20, alpha=0.4)

plt.yticks([])
plt.xticks(df["Year"])

plt.show()

提供:

enter image description here

但是我不确定这是否是您数据的最佳可视化,或者确实是您的问题要求的。


1
投票
d = """Year   Total      Passed
2011       500              250
2012       350              150
2013       348              100"""
df = pd.read_csv(StringIO(d), sep='\\s+', header=0)


fig, ax = plt.subplots()
ax.scatter(df['Year'], [0]*len(df['Year']), s=5*df['Total'], label='Total')
ax.scatter(df['Year'], [0]*len(df['Year']), s=5*df['Passed'], label='Passed')
ax.set_xticks(df['Year'])
ax.set_xticklabels(df['Year'])
ax.margins(x=0.25)
ax.legend()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.