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()
最后结果: