cat*_*t84 7 python matplotlib kernel-density pandas seaborn
kind='kde'Pandas在绘图时提供。在我的设置中,我更喜欢 kde 密度。替代方案kind='histogram'提供了方向选项:orientation='horizontal',这对于我正在做的事情是绝对必要的。不幸的是,orientation不适用于 kde。
至少这是我认为会发生的事情,因为我得到了
in set_lineprops
raise TypeError('There is no line property "%s"' % key)
TypeError: There is no line property "orientation"
Run Code Online (Sandbox Code Playgroud)
是否有任何直接的替代方法可以像绘制直方图一样轻松地水平绘制kde?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.ion()
ser = pd.Series(np.random.random(1000))
ax1 = plt.subplot(2,2,1)
ser.plot(ax = ax1, kind = 'hist')
ax2 = plt.subplot(2,2,2)
ser.plot(ax = ax2, kind = 'kde')
ax3 = plt.subplot(2,2,3)
ser.plot(ax = ax3, kind = 'hist', orientation = 'horizontal')
# not working lines below
ax4 = plt.subplot(2,2,4)
ser.plot(ax = ax4, kind = 'kde', orientation = 'horizontal')
Run Code Online (Sandbox Code Playgroud)
pandas.Series.plot.kde没有更改绘图方向的选项。scipy.stats.gaussian_kde计算值,并将它们绘制在带有 的线上matplotlib.axes.Axes.plot。seaborn.kdeplot是一个选项。gaussian_kde.plot.kde和都在幕后使用sns.kdeplotimport pandas as pd
import numpy as np
import seaborn as sns
from scipy.stats import gaussian_kde
# crate subplots and don't share x and y axis ranges
fig, axes = plt.subplots(2, 2, figsize=(10, 10), sharex=False, sharey=False)
# flatten the axes for easy selection from a 1d array
axes = axes.flat
# create sample data
np.random.seed(2022)
ser = pd.Series(np.random.random(1000)).sort_values()
# plot example plots
ser.plot(ax=axes[0], kind='hist', ec='k')
ser.plot(ax=axes[1], kind='kde')
ser.plot(ax=axes[2], kind='hist', orientation='horizontal', ec='k')
# 1. create kde model
gkde = gaussian_kde(ser)
# 2. create a linspace to match the range over which the kde model is plotted
xmin, xmax = ax2.get_xlim()
x = np.linspace(xmin, xmax, 1000)
# 3. plot the values
axes[3].plot(gkde(x), x)
# Alternatively, use seaborn.kdeplot and skip 1., 2., and 3.
# sns.kdeplot(y=ser, ax=axes[3])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1433 次 |
| 最近记录: |