Python中的 seaborn.lineplot() 方法
Seaborn 是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。颜色突出,层次很好地融合在一起,轮廓贯穿始终,整体包装不仅具有良好的美学品质,而且还为我们提供了有意义的见解。
seaborn.lineplot()
画一个可能有几个语义分组的线图。可以使用色调、大小和样式参数为数据的不同子集显示 x 和 y 之间的关系。这些参数控制用于识别不同子集的视觉语义。通过使用所有三种语义类型可以独立显示多达三个维度,但这种情节风格可能难以解释并且通常无效。使用冗余语义(即同一变量的色调和样式)有助于使图形更易于访问。
Syntax : sns.lineplot(x=None, y=None, hue=None, size=None, style=None, data=None, palette=None, hue_order=None, hue_norm=None, sizes=None, size_order=None, size_norm=None, dashes=True, markers=None, style_order=None, units=None, estimator=’mean’, ci=95, n_boot=1000, sort=True, err_style=’band’, err_kws=None, legend=’brief’, ax=None, **kwargs,)
Parameters:
x, y: Input data variables; must be numeric. Can pass data directly or reference columns in data.
hue: Grouping variable that will produce lines with different colors. Can be either categorical or numeric, although color mapping will behave differently in latter case.
style: Grouping variable that will produce lines with different dashes and/or markers. Can have a numeric dtype but will always be treated as categorical.
data: Tidy (“long-form”) dataframe where each column is a variable and each row is an observation.
markers: Object determining how to draw the markers for different levels of the style variable.
legend: How to draw the legend. If “brief”, numeric “hue“ and “size“ variables will be represented with a sample of evenly spaced values.
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("iris")
# draw lineplot
sns.lineplot(x="sepal_length", y="sepal_width", data=data)
plt.show()
Python3
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("tips")
# draw lineplot
# hue by sex
# style to hue
sns.lineplot(x="total_bill", y="size",
hue="sex", style="sex",
data=data)
plt.show()
输出 :
示例 2:
Python3
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("tips")
# draw lineplot
# hue by sex
# style to hue
sns.lineplot(x="total_bill", y="size",
hue="sex", style="sex",
data=data)
plt.show()
输出 :