用于显示数据在X轴连续数据段内的分布状况;直方图的变种,使用平滑曲线来绘制数值水平,从而得出更平滑的分布;较直方图,核密度估计图,不受所使用分组数量的影响,更好地界定分布形状;
在概率论中用来估计未知的密度函数,属于非参数检验方法之一;由Rosenblatt和Emanuel Parzen提出,又名Parzen窗(Parzen windows);
采用平滑的峰值函数“核”来拟合观察到的数据点,从而对真实的概率分布曲线进行模拟;核密度估计是一种用于估计概率密度函数的非参数方法;
plotnine包提供geomdensity()函数,绘制核密度估计图;geomdensity()函数的主要参数是带宽(bw)和核函数(kernel);核函数默认为高斯核函数“gaussian”;
峰峦图也可以应用于多数据系列的核密度估计可视化;X轴对应平均温度的数值范围;Y轴对应不同的月份;每个月份的核密度估计数值映射到颜色;可以很好地展示多数据系列的核密度估计结果;
joypy包提供了joyplot()函数,根据数据可以直接绘制不同颜色的核密度估计峰峦图;带有颜色渐变映射的核密度估计峰峦图,可以使用plotnine包的geomlinerange()函数和geomline()函数结合实现;绘制前需要先使用sklearn包的KernelDensity()函数求取每个月份的核密度估计曲线;再根据核密度估计数据绘制峰峦图;
import pandas as pd
from plotnine import *
df=pd.read_csv('d:\python\out\HistDensityD.csv')
#file.close() # 关闭文件
#多数剧系列核密度估计图
base_density=(ggplot(df, aes(x='MXSPD', fill='Location'))+
geom_density(bw=1,alpha=0.55,colour="black",size=0.25,kernel="gaussian")+
scale_fill_hue(s = 0.90, l = 0.65, h=0.0417,color_space='husl')+
theme(
text=element_text(size=13,color="black"),
plot_title=element_text(size=15,family="myfont",face="bold.italic",hjust=.5,color="black"),#,
legend_position=(0.7,0.75),
legend_background = element_blank(),
aspect_ratio =1.15,
figure_size=(5,5)
))
print(base_density)
import pandas as pd
import numpy as np
import joypy
import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns
plt.rc('font',family='Times New Roman')
#
sns.set_context("notebook", font_scale=1.5,
rc={'font.size': 12,
'axes.labelsize': 20, 'legend.fontsize':15,
'xtick.labelsize': 15,'ytick.labelsize': 15})
df = pd.read_csv("d:\python\out\lincoln_weather.csv")
Categories=['January', 'February', 'March', 'April', 'May', 'June','July',
'August','September', 'October', 'November','December']
df['Month']=df['Month'].astype("category",Categories)
fig, axes = joypy.joyplot(df, column=["Mean.Temperature..F."],
by="Month", ylim='own',colormap=cm.Spectral_r,
alpha= 0.9,figsize=(6,5))
plt.xlabel("Mean Temperature",{'size': 15 })
plt.ylabel("Month",{'size': 15 })
#axes.set_axis_labels("Mean Temperature", "Month")
plt.show()
fig.savefig('d:\python\out\joyplot.pdf')
(base) C:\Users\Administrator>pip install joypy
Collecting joypy
Downloading joypy-0.2.2-py2.py3-none-any.whl (8.3 kB)
Requirement already satisfied: scipy>=0.11.0 in c:\users\administrator\appdata\r
oaming\python\python37\site-packages (from joypy) (1.5.1)
Requirement already satisfied: pandas>=0.20.0 in c:\users\administrator\appdata\
roaming\python\python37\site-packages (from joypy) (1.0.5)
Requirement already satisfied: numpy in c:\anaconda3\lib\site-packages (from joy
py) (1.19.0)
Requirement already satisfied: matplotlib in c:\anaconda3\lib\site-packages (fro
m joypy) (3.3.0)
Requirement already satisfied: python-dateutil>=2.6.1 in c:\anaconda3\lib\site-p
ackages (from pandas>=0.20.0->joypy) (2.7.3)
Requirement already satisfied: pytz>=2017.2 in c:\anaconda3\lib\site-packages (f
rom pandas>=0.20.0->joypy) (2018.5)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in c:\an
aconda3\lib\site-packages (from matplotlib->joypy) (2.2.0)
Requirement already satisfied: cycler>=0.10 in c:\anaconda3\lib\site-packages (f
rom matplotlib->joypy) (0.10.0)
Requirement already satisfied: pillow>=6.2.0 in c:\anaconda3\lib\site-packages (
from matplotlib->joypy) (7.2.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\anaconda3\lib\site-packag
es (from matplotlib->joypy) (1.0.1)
Requirement already satisfied: six>=1.5 in c:\anaconda3\lib\site-packages (from
python-dateutil>=2.6.1->pandas>=0.20.0->joypy) (1.11.0)
Requirement already satisfied: setuptools in c:\anaconda3\lib\site-packages (fro
m kiwisolver>=1.0.1->matplotlib->joypy) (40.2.0)
Installing collected packages: joypy
Successfully installed joypy-0.2.2
(base) C:\Users\Administrator>