如何以水平方向绘制pandas kde

nhaq1z21  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(96)

Pandas在绘图时提供kind='kde'。在我的设置中,我更喜欢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"

字符串
是否有任何直接的替代方案可以像绘制 * 直方图 * 一样简单地绘制 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')


的数据

zsohkypk

zsohkypk1#

import 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])

字符串


的数据

相关问题