Python 以阈值两侧的不同颜色(颜色)显示matplotlib violinplot的各个部分

Python 以阈值两侧的不同颜色(颜色)显示matplotlib violinplot的各个部分,python,matplotlib,plot,Python,Matplotlib,Plot,我有一些代码以小提琴情节的形式表达选举结果的平衡: plt.plot((0, 0), (0.85, 1.15), 'k-') p = plt.violinplot(np.array(results['Balance']),vert=False, bw_method='scott', widths=0.2,showextrema=False) 这将返回一个如下所示的绘图: 我想把这幅图涂在“决定线”的两边,以反映投票意向。类似于下图所示的实体模型: 我试着独立地绘制两个不同的集合, e、 g

我有一些代码以小提琴情节的形式表达选举结果的平衡:

plt.plot((0, 0), (0.85, 1.15), 'k-')
p = plt.violinplot(np.array(results['Balance']),vert=False, bw_method='scott', widths=0.2,showextrema=False)
这将返回一个如下所示的绘图:

我想把这幅图涂在“决定线”的两边,以反映投票意向。类似于下图所示的实体模型:

我试着独立地绘制两个不同的集合, e、 g

p=plt.violinplot(np.array(results[results['Balance']>=0]['Balance']),vert=False,bw_method='scott',widths=0.2,showerema=False)

n=plt.violinplot(np.array)(results[results['Balance']]问题是,如果数据被剪切,violinplot将计算不同的小提琴。因此,需要在分隔线的两侧使用相同的小提琴

import matplotlib.pyplot as plt
import matplotlib.patches
import numpy as np
import pandas as pd

#generate some data
a = np.log(1+np.random.poisson(size=1500))
b = 0.2+np.random.rand(1500)*0.3
c = 0.1+np.random.rand(1500)*0.6
results=pd.DataFrame({'Balance':np.r_[a*b,-a*c]})

#create figure and axes
fig, ax=plt.subplots()
# sep is the point where the separation should occur
sep = -0.05
plt.plot((sep, sep), (0.85, 1.15), 'k-')
# plot the violin
p = plt.violinplot(np.array(results['Balance']),vert=False,
                   bw_method='scott', widths=0.2,showextrema=False)
# obtain path of violin surrounding
path = p['bodies'][0].get_paths()[0]

#create two rectangles left and right of the separation line
r =  matplotlib.patches.Rectangle((results['Balance'].min(),0.85), 
                  width=sep-results['Balance'].min(), height=0.3, facecolor="r")
r2 =  matplotlib.patches.Rectangle((sep,0.85), 
                  width=results['Balance'].max()-sep, height=0.3, facecolor="b")
# clip the rectangles with the violin path
r.set_clip_path(path, transform=ax.transData)
r2.set_clip_path(path, transform=ax.transData)
ax.add_patch(r)
ax.add_patch(r2)

#optionally add edge around violin.
s = matplotlib.patches.PathPatch(path, linewidth=1, edgecolor="k", fill=False)
ax.add_patch(s)
plt.show()
一个想法是使用小提琴的路径,可以通过
path=p['bodies'][0]获得。get_paths()[0]
,在分隔线的任一侧剪切出两个不同颜色的、绘图填充矩形的一部分

import matplotlib.pyplot as plt
import matplotlib.patches
import numpy as np
import pandas as pd

#generate some data
a = np.log(1+np.random.poisson(size=1500))
b = 0.2+np.random.rand(1500)*0.3
c = 0.1+np.random.rand(1500)*0.6
results=pd.DataFrame({'Balance':np.r_[a*b,-a*c]})

#create figure and axes
fig, ax=plt.subplots()
# sep is the point where the separation should occur
sep = -0.05
plt.plot((sep, sep), (0.85, 1.15), 'k-')
# plot the violin
p = plt.violinplot(np.array(results['Balance']),vert=False,
                   bw_method='scott', widths=0.2,showextrema=False)
# obtain path of violin surrounding
path = p['bodies'][0].get_paths()[0]

#create two rectangles left and right of the separation line
r =  matplotlib.patches.Rectangle((results['Balance'].min(),0.85), 
                  width=sep-results['Balance'].min(), height=0.3, facecolor="r")
r2 =  matplotlib.patches.Rectangle((sep,0.85), 
                  width=results['Balance'].max()-sep, height=0.3, facecolor="b")
# clip the rectangles with the violin path
r.set_clip_path(path, transform=ax.transData)
r2.set_clip_path(path, transform=ax.transData)
ax.add_patch(r)
ax.add_patch(r2)

#optionally add edge around violin.
s = matplotlib.patches.PathPatch(path, linewidth=1, edgecolor="k", fill=False)
ax.add_patch(s)
plt.show()

太美了!我喜欢它,这正是我想要的-谢谢你的回答。