Python 画小提琴图

from plotnine import *

d1 = np.random.randn(271,1)
d2 = np.random.randn(300,1)


categories = ['c1']*d1.shape[0] + ['c2']*d2.shape[0]
# title_name = 'Mean' + name + 'score'
df = pd.DataFrame({
    'category': categories,
    'value': np.vstack((d1, d2)).flatten(),
    
})
colors = ['#FFA38F']*d1.shape[0] + ['#96C9F4']*d2.shape[0]

p = (
    ggplot(df, aes(x='category', y='value', fill='category')) +
    geom_violin(aes(fill='category', color='category'), position=position_nudge(x=0), trim=True, adjust=2, alpha=0.2, size = 1.5) +
    geom_point(aes(color=colors), stroke=0, size=2, position=position_jitter(width=0.15)) +
    geom_boxplot(aes(fill='category', color='category'), width=0.2, position=position_nudge(x=0), size=1, outlier_alpha=0.5) +
    
    theme(
        legend_position="none",
        panel_spacing_x=10, figure_size=(5, 3),
        aspect_ratio=0.7,
        axis_text_x=element_text(family='Times New Roman', size=20,weight='bold',color='#000000',rotation=0),
        axis_text_y=element_text(family='Times New Roman', size=20,weight='bold',color='#000000',rotation=0),
        
        axis_title_y=element_text(family='Times New Roman', size=20, weight='bold',rotation=90),
        axis_title=element_blank(),  # Remove axis titles
        
        panel_grid_major=element_blank(),   # Remove major grid lines
        panel_grid_minor=element_blank(),   # Remove minor grid lines
        panel_background=element_blank(),   # Remove panel background
        strip_background=element_blank(),   # Remove strip background
        plot_background=element_blank(),
        
        axis_line=element_line(color='black', size=2),
        axis_ticks_major=element_line(color='black',size=2),  # Set the thickness of the major ticks
        axis_ticks_length_major=6  # Set the length of the major ticks
    ) +
    scale_fill_manual(values=['#FEDAE3', '#C3E1F9']) +
    scale_color_manual(values=['#C80036', '#3FA2F6'])
)
# ggsave(p, rf"difference.svg", dpi=600)
p.draw()

最后结果:

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值