Python – seaborn.swarmplot() 方法
先决条件: Seaborn 的基础知识
Seaborn 是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。精心设计的可视化有一些非凡之处。颜色突出,层次很好地融合在一起,轮廓贯穿始终,整体包装不仅具有良好的美学品质,而且还为我们提供了有意义的见解。
seaborn.swarmplot()
绘制具有非重叠点的分类散点图。群体图可以单独绘制,但如果您想要显示所有观察结果以及基础分布的一些表示,它也是箱形图或小提琴图的一个很好的补充。正确排列点需要在数据和点坐标之间进行准确的转换。这意味着必须在绘制绘图之前设置非默认轴限制。
Syntax : seaborn.swarmplot(parameters)
Parameters :
- x, y, hue : Inputs for plotting long-form data.
- data : Dataset for plotting.
- order, hue_order : Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
- dodge : To separate the strips for different hue levels along the categorical axis
- orient : Orientation of the plot (vertical or horizontal).
- color : Color for all of the elements, or seed for a gradient palette.
- palette : Colors to use for the different levels of the hue variable.
- size : Radius of the markers, in points.
- edgecolor : Color of the lines around each point.
- linewidth : Width of the gray lines that frame the plot elements.
- ax : Axes object to draw the plot onto, otherwise uses the current Axes.
- kwargs : Other keyword arguments
Returns : Returns the Axes object with the plot drawn onto it.
以下是上述方法的实现以及一些示例:
示例 1:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("tips")
# plot the swarmplot
# size set to 5
sns.swarmplot(x ="day", y = "total_bill",
data = data, size = 5)
plt.show()
输出 :
示例 2:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("tips")
# plot the swarmplot
# hue by size
# oriented to horizontal
sns.swarmplot(y = "day", x = "total_bill", hue = "size",
orient = "h", data = data)
plt.show()
输出 :
示例 3:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("tips")
# plot the swarmplot
# hue by smoker
# dodge = True
sns.swarmplot(x = "sex", y = "total_bill", hue = "smoker",
data = data, dodge = True)
plt.show()
输出 :
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。