import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
import seaborn as sns
sns.set() #切换到sns的默认运行配置
import warnings
warnings.filterwarnings('ignore')
sns.set_style('white')
plt.figure(figsize=(12,10))#绘制画布
sns.distplot(up_dis['HubDist'],hist = True,bins=17,kde = True,rug = True,
rug_kws = {'color':'b','lw':2,'alpha':0.5,'height':0.1} , # 设置数据频率分布颜色#控制是否显示观测的小细条(边际毛毯)
kde_kws={"color": "b", "lw": 1.5, 'linestyle':'-'})
sns.distplot(down_dis['HubDist'],hist = True,bins=17,kde = True,rug = True,
rug_kws = {'color':'orange','lw':2,'alpha':0.5} ,
kde_kws={"color": "orange", "lw": 1.5, 'linestyle':'-'})# 设置密度曲线颜色,线宽,标注、线形,#控制是否显示核密度估计图
x_major_locator=MultipleLocator(30)#把x轴的刻度间隔设置为30,并存在变量里
ax=plt.gca()#ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)#把x轴的主刻度设置为30的倍数
plt.xlim(0,540)
plt.xlabel('HubDist/m')
plt.legend(["up_dis","down_dis"])#添加图例
plt.grid(linestyle = '--') # 添加网格线
plt.title("Histogram & Density Estimation of Distances Between the Cluster Centres and Nearest Intersection",fontsize=16) # 添加图表名
plt.savefig(fname="聚类中心最邻近交叉口距离直方图&密度分布图sns.png")
用matpltlib也可以绘制出同样效果:
##两组数据重叠
plt.figure(figsize=(20,20))#绘制画布
data=pd.DataFrame({'up_dis':up_dis['HubDist'],'down_dis':down_dis['HubDist']})
xmajorLocator = MultipleLocator(30)
ax.xaxis.set_major_locator(xmajorLocator)
#绘制密度分布图
plt.figure(figsize=(12,10))
data.plot(kind = 'kde',color = ['c','r'],label = 'density')
plt.xlim(0,540) # 获取当前子图
plt.xticks(np.arange(0,540,30))
# ax.spines['right'].set_color('none') # 右边框设置无色
# ax.spines['top'].set_color('none') # 上边框设置无色
# 显示图例
plt.legend()
#绘制直方图
plt.hist(up_dis['HubDist'], 17, alpha=0.5, color ='c',label='up_dis',density = True)
plt.hist(down_dis['HubDist'], 17, alpha=0.5, color ='r',label='down_dis',density = True)
plt.xticks(np.arange(0,540,30))
plt.title("Histogram & Density Estimation of Distances Between the Cluster Centres and Nearest Intersection")
plt.legend()
##两组数据平铺分组
plt.figure(figsize=(20,20))#绘制画布
data=pd.DataFrame({'up_dis':up_dis['HubDist'],'down_dis':down_dis['HubDist']})
xmajorLocator = MultipleLocator(30)
ax.xaxis.set_major_locator(xmajorLocator)
#绘制密度分布图
plt.figure(figsize=(12,10))
data.plot(kind = 'kde',color = ['c','r'],label = 'density')
plt.xlim(0,540) # 获取当前子图
plt.xticks(np.arange(0,540,30))
# ax.spines['right'].set_color('none') # 右边框设置无色
# ax.spines['top'].set_color('none') # 上边框设置无色
# 显示图例
plt.legend()
#绘制直方图
plt.hist(x = data, # 绘图数据
bins = 17, # 指定直方图的条形数为20个
edgecolor = 'w', # 指定直方图的边框色
color = ['c','r'], # 指定直方图的填充色
label = ['up_dis','down_dis'], # 为直方图呈现图例,分别为上下行聚类中西你距离交叉口距离
density = True, # 是否将纵轴设置为密度,即频率
alpha = 0.6, # 透明度
rwidth = 1, # 直方图宽度百分比:0-1
stacked = False) # 当有多个数据时,是否需要将直方图呈堆叠摆放,默认水平摆放
plt.xticks(np.arange(0,540,30))
plt.xlabel('HubDist/m')
plt.title("Histogram & Density Estimation of Distances Between the Cluster Centres and Nearest Intersection")
plt.legend()
#保存图片
# plt.savefig(fname="聚类中心最邻近交叉口距离直方图&密度分布图.png",figsize=[30,30])