import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
iris = pd.read_csv('../Data/iris.csv')
Matplotlib:eventplot¶
matplotlib.axes.Axes.eventplot — Matplotlib 3.10.1 documentation
eventplot(positions, *, orientation='horizontal', lineoffsets=1, linelengths=1, linewidths=None, colors=None, alpha=None, linestyles='solid', data=None, **kwargs)
在给定位置绘制相同的平行线。这种类型的图表在神经科学中常用于表示神经事件,通常被称为尖峰阵列、点阵列或阵列图。然而,在任何需要展示多组离散事件的时机或位置的情况下,它都是有用的,例如展示人们在每个月每天到达商家的到达时间或上个世纪每年飓风的日期。
如果提供data参数,以下参数也接受一个字符串s,如果s是data中的键,则解释为data[s]:positions, lineoffsets, linelengths, linewidths, colors, linestyles。
-
**positions:**一个一维数组定义了一个事件序列的位置。可以将多个事件组作为数组类型对象的列表传递。可以通过传递值列表给
lineoffsets、linelengths、linewidths、colors和linestyles来独立地设置每个组的样式。注意,该参数可以是二维数组,但在实际应用中,由于不同的事件组通常具有不同的数量,因此通常会使用不同长度的数组列表而不是二维数组。 -
**orientation:**事件序列的方向。
'horizontal'(默认),事件水平排列,指示线垂直;'vertical':,事件垂直排列,指示线水平。 -
**lineoffsets:**线的中心在垂直于
orientation的方向上的偏移量。如果positions是二维的,则这可以是一个与positions长度匹配的序列。默认为1。 -
**linestyles:**线型。如果
positions是二维的,则这可以是一个与positions长度匹配的序列。默认为'solid'。 -
**linelengths:**线的总高度(即线从
lineoffset - linelength/2延伸到lineoffset + linelength/2)。如果positions是二维的,则这可以是一个与positions长度匹配的序列。默认为1。 -
**linewidths:**线宽(以点为单位)。如果
positions是二维的,则这可以是一个与positions长度匹配的序列。默认rcParams["lines.linewidth"]=1.5。 -
**colors:**线的颜色。如果
positions是二维的,则这可以是一个与positions长度匹配的序列。默认rcParams["lines.color"]='C0'。 -
**alpha:**线的透明度。如果
positions是二维的,则这可以是一个与positions长度匹配的序列。默认为1。
**kwargs其他关键字参数传递给matplotlib.collections.LineCollection()。
fig = plt.figure(figsize=(8, 8), dpi=100, layout="constrained")
ax = fig.add_subplot(1, 1, 1)
a = ax.eventplot([iris['sepal_length'], iris['sepal_width'], iris['petal_length'], iris['petal_width']],
orientation='horizontal',
lineoffsets=[1, 2, 3, 4],
linestyles=['-', '--', '-.', ':'],
linelengths=[0.1, 0.2, 0.3, 0.4],
linewidths=[1, 2, 3, 4],
colors=['r', 'g', 'b', 'y'],
alpha=[0.3, 0.4, 0.5, 0.6],
)
"""
[<matplotlib.collections.EventCollection at 0x1674502ee40>,
<matplotlib.collections.EventCollection at 0x16745060d10>,
<matplotlib.collections.EventCollection at 0x16745062c60>,
<matplotlib.collections.EventCollection at 0x16745078d70>]
"""
a[0].set_label('sepal length')
a[1].set_label('sepal width')
a[2].set_label('petal length')
a[3].set_label('petal width')
ax.legend()
plt.show()
Seaborn:rugplot¶
seaborn.rugplot — seaborn 0.13.2 documentation
rugplot(data=None, *, x=None, y=None, hue=None, height=0.025, expand_margins=True, palette=None, hue_order=None, hue_norm=None, legend=True, ax=None, **kwargs)
通过在 x 轴和 y 轴上绘制刻度来绘制边缘分布图。此函数旨在通过以不显眼的方式显示单个观测值的位置来补充其他图表。
-
**height:**每个rug元素覆盖的轴范围比例。可以是负数。
-
**expand_margins:**如果为
True,则通过rug的高度增加Axes的边距,以避免与其他元素重叠。
**kwargs其他关键字参数传递给matplotlib.collections.LineCollection()。
fig = plt.figure(figsize=(8, 8), dpi=100, layout="constrained")
ax = fig.add_subplot(1, 1, 1)
sns.rugplot(data=iris,
x='sepal_length',
y='sepal_width',
hue="species",
hue_order=["versicolor", "setosa", "virginica"],
palette={"versicolor": '#9673f9', "setosa": '#00f9bb', "virginica": '#98f907'},
height=0.025,
expand_margins=False,
ax=ax
)
"""
<Axes: xlabel='sepal_length', ylabel='sepal_width'>
"""
sns.scatterplot(data=iris,
x='sepal_length',
y='sepal_width',
hue="species",
hue_order=["versicolor", "setosa", "virginica"],
palette={"versicolor": '#9673f9', "setosa": '#00f9bb', "virginica": '#98f907'},
ax=ax
)
plt.show()


