python中的连续violineplot

2024-10-03 13:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何命名,但我需要python中的“连续violineplot”之类的东西。Violineplot(如seaborn)通常只适用于离散类,分布密度的频率沿x轴方向绘制(并镜像为violine)。你知道吗

如果我有一个时间点,在每个点上有一个密度曲线,我可以把频率显示为一种颜色,然后在平均曲线周围得到一个连续的条纹/管/区域,显示彩色密度。你知道吗

这在python中是可能的吗?你知道吗


Tags: 区域镜像颜色时间绘制seaborn方向命名
1条回答
网友
1楼 · 发布于 2024-10-03 13:22:18

好的,我为我的问题建立了一个解决方案,我称之为“滑动密度图”(只要没有其他名称)。你知道吗

代码如下:

#!/usr/bin/python
# -*- coding: latin-1 -*-

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from   scipy.stats import gaussian_kde

# Generate artificial data
tmin = 0
tmax = 1000
tN   = 1001
t0   = np.linspace(tmin,tmax,num=tN) # timesteps
y0   = np.sin(t0/1000.0*3.0*np.pi)

rmin = 0
rmax = 100
rN   = 101
r0   = np.linspace(rmin,rmax,num=rN) # runs of simulation (e.g.)

data  = [t0]
names = ['time']

for r in r0:

  name = "run%03i" % int(r)

  y1 = y0 + np.sin((r+t0/10.0)/100.0*np.pi)

  names.append(name)
  data.append(y1)

data = np.array(data)    
data = pd.DataFrame(data.T,columns=names)

# prepare kernel density estimation for each timestep
mybins_kde = np.linspace(-4,4,320)
sdens   = []
select = [x for x in data.columns if x != "time"]
data1  = data[select] 

# do kde
for i in range(tmin,tmax+1):
  y   = data1.iloc[i]
  bw  = (0.05/y.std(ddof=1))
  kde = gaussian_kde(y,bw_method=bw)
  h   = [kde.evaluate(mybins_kde)]
  sdens.append(h[0])

# prepare imshow
img = np.array(sdens).T

# start plot
fig = plt.figure(figsize=(15,10))

# plot sliding density as image
plt.imshow(img,aspect='auto',cmap="gray_r",interpolation="bilinear",
               extent=[tmin,tmax, -4, 4],origin='lower')

# do some statistics               
mean = np.mean(data1,axis=1)
medi = np.median(data1,axis=1)
pc10 = np.percentile(data1,10,axis=1)
pc90 = np.percentile(data1,90,axis=1)

# plot statistics

plt.plot(t0,mean,label='Mean')
plt.plot(t0,medi,label='Median')
plt.plot(t0,pc10,label='10%-Percentil')
plt.plot(t0,pc90,label='90%-Percentil')

# beautify plot
plt.xlim(tmin,tmax)   
plt.legend(loc='upper right')         
plt.xlabel("time")
plt.ylabel("value")
plt.title("sliding density plot for 100 sets of data")
plt.grid(linestyle='dashed',zorder=0) 

# plot some example densities
maxy    = 0
ax2     = fig.add_axes([0.16,0.14,0.25,0.25])
inset1  = np.array(sdens[150])
inset1 /= np.sum(inset1)
ax2.plot(mybins_kde,inset1,label="t=150")
inset2  = np.array(sdens[500])
inset2 /= np.sum(inset2)
ax2.plot(mybins_kde,inset2,label="t=500")
inset3  = np.array(sdens[980])
inset3 /= np.sum(inset3)
ax2.plot(mybins_kde,inset3,label="t=980")

# beautify inset plot
maxy = max([np.max(inset1),np.max(inset2),np.max(inset3)])
ax2.set_title("Example for densities at time t")
ax2.legend(loc="upper left")
ax2.set_ylim(0,round(1.5*maxy,2))

# show plot
plt.show()

嗯,我肯定,这不是最好的和美丽的代码,但它做了我需要的。你知道吗

结果图如下: sliding density plot

即使数据和代码不是最佳的,我想人们可以理解我的问题和我的解决方案。你知道吗

相关问题 更多 >