I am trying to create a violin plot in seaborn and would like to have the axis revert.
The full code for plotting violin is here 1. For me it would be more useful if I could swap the violin horizontally, so the critical_value
lies on the x-axis.
I tried to find some solution, there is some discussion on R Library, however could not find anything concerning python.
I would appreciate any help.
Thank you,
1
def draw_2d_violin_v3(data, hue_feature, feature, type='v', fig_width=8, fig_height=6, labels=None, title_label=None, label_font_size=12, save=False, save_as=None, write_text=False, text=None, text_col='r', text_x=None, text_y=None):
fig, ax = plt.subplots(figsize=(fig_width, fig_height))
# Group the data by the feature column
grouped_data = data.groupby(feature)
sns.violinplot(y=data[feature], x=data[hue_feature])#, color='skyblue')
# Add a red vertical line at a specific x-value
target_value = 0.5
if type == 'v':
ax.axvline(x=target_value, color='red', linestyle='--', linewidth=1)
elif type == 'h':
ax.axhline(y=target_value, color='red', linestyle='--', linewidth=1)
# Customize the plot
ax.set_title(title_label, fontsize=label_font_size)
ax.set_xlabel(labels[0], fontsize=label_font_size)
ax.set_ylabel(labels[1], fontsize=label_font_size)
plt.grid(True)
if write_text:
ax.text(text_x, text_y, f"{text}", color=text_col, fontsize=12)
# Save the plot
if save:
plt.savefig(f"{PLOT_DIR}/{save_as}", dpi=300)
plt.show()