-5- 绘制小提琴图
def Violin_chart(self, data_list, xticks_name_List, title, savepath, show=False):
'''
Function:This script is used to draw a violin plot.
Athor:xiaohuoya
Date:2020-05-28
Eg: -1- data_list: the data list
-2- xticks_name_List: the xticks name list
-3- savepath: the figure save path
'''
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(1, 1, constrained_layout=True, figsize=(8, 5))
plt.style.use(self.pltstyle)
sns.violinplot(cut=0, data=data_list, width=self.wd)
plt.xticks(self.xticks_list[0:len(data_list)],
xticks_name_List, rotation=self.rot)
ax.set_title(title, {'size': self.fs, 'color': self.cl}, loc='left')
if show == True:
plt.savefig(savepath, dpi=self.dpi)
plt.show()
elif show == False:
plt.savefig(savepath, dpi=self.dpi)
1.此代码有些许简陋,仍处于完善之中,不过可以提供一种绘图思路
2.补充一下默认设置:
class Draw:
def __init__(self):
self.pltstyle = 'ggplot'
self.wd = 0.5
self.xticks_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
self.rot = 45
if (__name__ == '__main__'):
D.rot = 0
D.Violin_chart( [pr_6,pr_6_obs,pr,pr_obs],['pr_6','pr_6_obs','pr_7','pr_7_obs'],'Made in xiaohuoya','./violin.pdf',show=False)
微信图片_20220528112838.png