如何使用 seaborn 制作气泡图

Posted

技术标签:

【中文标题】如何使用 seaborn 制作气泡图【英文标题】:How to make a bubble graph using seaborn 【发布时间】:2021-04-28 21:55:05 【问题描述】:
   import matplotlib.pyplot as plt
    import numpy as np
    # data
    x=["IEEE", "Elsevier", "Others"]
    y=[7, 6, 2]
    import seaborn as sns
    plt.legend()
    plt.scatter(x, y, s=300, c="blue", alpha=0.4, linewidth=3)
    plt.ylabel("No. of Papers")
    plt.figure(figsize=(10, 4)) 

我想制作一个如图所示的图表。我不确定如何为期刊和会议类别提供数据。 (目前,我只包括一个)。另外,我不确定如何为每个类别添加不同的颜色。

【问题讨论】:

这能回答你的问题吗? pyplot scatter plot marker size 【参考方案1】:

你可以试试这个代码 sn-p 来解决你的问题。

- 我修改了你的数据格式,建议你使用 pandas 数据可视化。

- 我添加了一个字段以更有效地可视化数据。

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd

# data
x=["IEEE", "Elsevier", "Others", "IEEE", "Elsevier", "Others"]
y=[7, 6, 2, 5, 4, 3]
z=["conference", "journal", "conference", "journal", "conference", "journal"]

# create pandas dataframe
data_list = pd.DataFrame(
    'x_axis': x,
     'y_axis': y,
     'category': z
    )

# change size of data points
minsize = min(data_list['y_axis'])
maxsize = max(data_list['y_axis'])

# scatter plot
sns.catplot(x="x_axis", y="y_axis", kind="swarm", hue="category",sizes=(minsize*100, maxsize*100), data=data_list)
plt.grid()

【讨论】:

感谢您的帮助。我想知道是否有可能重叠的气泡也可以显示在图形区域中。 是的,这是可能的,你可以通过改变气泡的大小来做到这一点,在这段代码中,我将值硬编码为 100 为“sizes=(minsize*100, maxsize*100)”,你可以根据您的需要进行操作。 谢谢。我希望你能理解我的问题。我想说的是,如果对于某些值,两个类别的值都为 1,那么在这种情况下,圆圈将重叠,因此一种方法是使用不透明度使圆圈可见(alpha=0.4)。还有其他解决方案吗? 另外,要正确映射数据 z 应该是 z=["conference", "conference", "conference", "journal", "journal", "journal"] 在这种情况下,您可以使用这些示例seaborn.pydata.org/generated/seaborn.scatterplot.html【参考方案2】:

如何创建具有正确气泡大小且没有重叠的图表

Seaborn stripplotswarmplot(或 sns.catplot(kind=strip or kind=swarm))提供了方便的 dodge 参数,可以防止气泡重叠。唯一的缺点是 size 参数将单个大小应用于所有气泡,而 sizes 参数(在另一个答案中使用)在这里没有用。它们不像scatterplotssize 参数那样工作。因此,每个气泡的大小必须在生成绘图后进行编辑:

import numpy as np     # v 1.19.2
import pandas as pd    # v 1.1.3
import seaborn as sns  # v 0.11.0

# Create sample data
x = ['IEEE', 'Elsevier', 'Others', 'IEEE', 'Elsevier', 'Others']
y = np.array([7, 6, 3, 7, 1, 3])
z = ['conference', 'conference', 'conference', 'journal', 'journal', 'journal']
df = pd.DataFrame(dict(organisation=x, count=y, category=z))

# Create seaborn stripplot (swarmplot can be used the same way)
ax = sns.stripplot(data=df, x='organisation', y='count', hue='category', dodge=True)

# Adjust the size of the bubbles
for coll in ax.collections[:-2]:
    y = coll.get_offsets()[0][1]
    coll.set_sizes([100*y])

# Format figure size, spines and grid
ax.figure.set_size_inches(7, 5)
ax.grid(axis='y', color='black', alpha=0.2)
ax.grid(axis='x', which='minor', color='black', alpha=0.2)
ax.spines['bottom'].set(position='zero', color='black', alpha=0.2)
sns.despine(left=True)

# Format ticks
ax.tick_params(axis='both', length=0, pad=10, labelsize=12)
ax.tick_params(axis='x', which='minor', length=25, width=0.8, color=[0, 0, 0, 0.2])
minor_xticks = [tick+0.5 for tick in ax.get_xticks() if tick != ax.get_xticks()[-1]]
ax.set_xticks(minor_xticks, minor=True)
ax.set_yticks(range(0, df['count'].max()+2))

# Edit labels and legend
ax.set_xlabel('Organisation', labelpad=15, size=12)
ax.set_ylabel('No. of Papers', labelpad=15, size=12)
ax.legend(bbox_to_anchor=(1.0, 0.5), loc='center left', frameon=False);

或者,您可以将scatterplot 与方便的s 参数(或size)一起使用,然后编辑气泡之间的空间以重现缺少dodge 参数的效果(请注意, x_jitter 参数似乎没有效果)。这是一个使用与以前相同的数据但没有所有额外格式的示例:

# Create seaborn scatterplot with size argument
ax = sns.scatterplot(data=df, x='organisation', y='count',
                     hue='category', s=100*df['count'])
ax.figure.set_size_inches(7, 5)
ax.margins(0.2)

# Dodge bubbles
bubbles = ax.collections[0].get_offsets()
signs = np.repeat([-1, 1], df['organisation'].nunique())
for bubble, sign in zip(bubbles, signs):
    bubble[0] += sign*0.15


作为旁注,我建议您考虑为该数据绘制其他类型的图。分组条形图:

df.pivot(index='organisation', columns='category').plot.bar()

或者balloon plot(又名分类气泡图):

sns.scatterplot(data=df, x='organisation', y='category', s=100*count).margins(0.4)

为什么? 在气泡图中,计数使用 2 个视觉属性显示,i) y 坐标位置和 ii) 气泡大小。只有其中一个是真正需要的。

【讨论】:

嗨,在您的第一张图中,为什么长轴消失了?你能告诉我怎么带它们吗 @user3582228 您好,x 轴仍然存在,但已格式化为网格线,您可以通过删除线 ax.spines['bottom'].set(...) 将其设置回默认格式。如果删除sns.despine(left=True) 行,y 轴以及顶部和右侧的脊椎将再次出现。

以上是关于如何使用 seaborn 制作气泡图的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 seaborn 制作以下条形图? [复制]

如何使用 seaborn 和 ipywidgets 制作交互式条形图

如何在 python/matplotlib 中制作居中气泡图

如何使用 displot 在 python 中制作 seaborn 图,在其中我们计算一个字段中的唯一值而不是总行数?

如何制作图标大小取决于大小的图标气泡图?

如何在具有不同 Y 轴的同一个 seaborn 图中很好地制作条形图和线图?