seaborn学习笔记2.0

分类变量的可视化

1、stripplot()

stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
sns.stripplot(x='day', y='total_bill', data=tips)
plt.show()

 

其中jitter参数可以控制抖动程度,可以是bool型也可以是具体数值,抖动可以减少数据重叠

                          不使用抖动(jitter=False)                                           使用抖动(jitter=True)

当设置hue参数时,根据hue参数进行不同颜色显示,例如,设置hue='smoker'

当有hue参数时,设置dodge参数可以分离不用类别数据,例如,设置dodge=True

2、swamplot()

swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
sns.swarmplot(x='day', y='total_bill', data=tips, hue='sex')
plt.show()

当x为数值型变量

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax=sns.swarmplot(x='size', y='total_bill',data=tips.query("size != 7"))
plt.show()

其中tips.query("size != 7")表示绘制x中size != 7的数据

通过order设置x数据显示顺序,例如,设置order=[1, 3, 2, 4, 5, 6]

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax=sns.swarmplot(x='size', y='total_bill', order=[1, 3, 2, 4, 5, 6],data=tips.query("size != 7"))
plt.show()

可以看到size=2和size=3的顺序交换了

交换x,y轴数据对于当类别名称相对较长或有许多类别时有帮助

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax = sns.swarmplot(y='day', x='total_bill', data=tips)
plt.show()

也可以旋转一下坐标

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax = sns.swarmplot(x='size', y='total_bill', data=tips)
ax.set_xticklabels(ax.get_xticklabels(), rotation=-45)
plt.show()

 

3、boxplot()

boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)
import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax = sns.boxplot(x="day", y="total_bill", hue='smoker', dodge=True, data=tips)
ax.legend(shadow=False, fancybox=False, ncol=1, fontsize=10, loc='upper left')
plt.show()

其中hue参数,dodge参数的设置前面已经提到过,这里不再赘述

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset('tips')
ax = sns.boxplot(x="day", y="total_bill", hue='smoker', dodge=True, data=tips)
ax.legend(shadow=False, fancybox=False, ncol=1, fontsize=10, loc='upper left')
plt.show()

4、boxenplot()

 
boxenplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, k_depth='proportion', linewidth=None, scale='exponential', outlier_prop=None, ax=None, **kwargs)
 
import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
diamonds = sns.load_dataset("diamonds")
ax = sns.boxenplot(x="color", y="price", data=diamonds.sort_values("color"))
#ax.legend(shadow=True, fancybox=True, ncol=1, fontsize=10, loc='upper left')
plt.show()

 

 

 

5、violinplot()

violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
tips = sns.load_dataset("tips")
ax = sns.violinplot(y="total_bill", x="day", inner="stick", hue="sex", split=True, data=tips)
#ax.legend(shadow=True, fancybox=True, ncol=1, fontsize=10, loc='upper right')
plt.show()

inner参数可以设置为 {"box", "quartile", "point", "stick", None}

如果是box,绘制一个小型的箱线图,如果是quartiles,绘制四分位数分布,如果是point或者stick,画每一个数据,如果是None,不做任何修饰

6、barplot()

barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x000001FCA4D5E8C8>, ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
titanic = sns.load_dataset("titanic")
ax = sns.barplot(x="sex", y="survived", hue="class", data=titanic)
#ax.legend()
plt.show()

其中ci参数可以设置是否绘制error bar,如果为整数,则绘制估计数周围整数大小的置信区间,如果为sd,绘制标准差,如果为None,不绘制error bar。另外置信区间范围内数值是使用bootstrapping方法计算出来的

Bootstrapping算法,指的就是利用有限的样本资料经由多次重复抽样,重新建立起足以代表母体样本分布的新样本。bootstrapping方法的实现很简单,假设抽取的样本大小为n:

在原样本中有放回的抽样,抽取n次。每抽一次形成一个新的样本,重复操作,形成很多新样本,通过这些样本就可以计算出样本的一个分布。

7、countplot()

countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
titanic = sns.load_dataset("titanic")
ax = sns.countplot(x="deck", palette="ch:.25", data=titanic)
#ax.legend()
plt.show()

countplot显示的是观察值,而不是统计值

8、pointplot()

pointplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x000002465DF2E8C8>, ci=95, n_boot=1000, units=None, markers='o', linestyles='-', dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, capsize=None, ax=None, **kwargs)

import seaborn as sns
from matplotlib import pyplot as plt

sns.set_style('darkgrid')
titanic = sns.load_dataset("titanic")
ax = sns.pointplot(x="class", y="survived", hue="sex",
                   palette={"male": "g", "female": "m"},
                   markers=["^", "o"], linestyles=["-", "--"],
                   data=titanic)
#ax.legend()
plt.show()

通过设置palette,不同marker和不同linestyle来区分不同类别

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
强化学习seaborn 是两个不同的主题,但是可以结使用来绘制强化学习的曲。以下是一个使用 seaborn 绘制强化学习曲线的基本示例: 首先,确保你已经安装了 seaborn 库。如果没有安装,可以使用以下命令进行安装: ``` pip install seaborn ``` 然后,导入 seaborn 和其他必要的库: ```python import seaborn as sns import matplotlib.pyplot as plt ``` 假设你有一个强化学习任务,你已经运行了多个实验,并且每个实验记录了每个回合的奖励值。你可以将这些奖励值绘制成曲线,以观察强化学习算法的学习进展。 下面是一个简单的示例代码,它使用 seaborn 绘制了三个实验的奖励曲线: ```python # 假设你有三个实验的奖励数据 experiment1_rewards = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] experiment2_rewards = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] experiment3_rewards = [0, 3, 6, 9, 12, 15, 18, 21, 24, 27] # 创建一个包含所有实验奖励值的数据框 data = { 'Experiment 1': experiment1_rewards, 'Experiment 2': experiment2_rewards, 'Experiment 3': experiment3_rewards } df = pd.DataFrame(data) # 使用 seaborn 绘制曲线 sns.lineplot(data=df) # 显示图形 plt.show() ``` 运行这段代码,你将会得到一个包含三个实验曲线的图形。 这只是一个简单的示例,你可以根据自己的需求调整绘图参数和数据格式等。希望这个示例能够帮助你开始使用 seaborn 绘制强化学习曲线。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值