When I try to combine a regplot
and a violinplot
, they are drawn at a different positions.
df = pd.DataFrame({'Magnitudes': magnitudes, 'Lengths': np.log10(lengths), 'Widths': np.log10(widths)})
# Colors for violins
palette = sns.color_palette("Set2", len(fullpaths))
sns.set_style("darkgrid", {"grid.color": ".6", "grid.linestyle": ":"})
# Crear gráficos de violín con regresión lineal y personalización de color
plt.figure()
plt.subplot(2, 1, 1)
sns.regplot(x='Magnitudes', y='Lengths', data=df,scatter_kws={"alpha" : 0.0})
sns.violinplot(x='Magnitudes', y='Lengths', data=df, palette=palette, alpha=0.5,hue='Magnitudes',legend=False)
plt.title('Violin Plot of Lengths vs Magnitude')
plt.xlabel('Magnitude')
plt.ylabel('Lengths')
plt.subplot(2, 1, 2)
sns.regplot(x='Magnitudes', y='Widths', data=df,scatter_kws={"alpha" : 0.0})
sns.violinplot(x='Magnitudes', y='Widths', data=df, palette=palette, alpha=0.5,hue='Magnitudes',legend=False)
plt.title('Violin Plot of Widths vs Magnitude')
plt.xlabel('Magnitude')
plt.ylabel('Widths')
Alex Villarroel Carrasco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
By default, seaborn draws categorical plots, such as violin plots (or boxplots, bar plots, …) with a categorical x-axis. Internally, they are at positions 0, 1, 2, ...
. Since Seaborn 0.13, these functions accept a parameter native_scale=True
, which positions the violins at their true numerical position (this only works for a numerical x-axis). If these positions aren’t nicely incrementing with the same distance, the minimum distance between two violins is used as reference for their widths.
Here is an example, including dummy test data, and using matplotlib’s “modern” object-oriented interface (strongly recommended when there is more than one subplot).
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# create some dummy test data
magnitudes = np.repeat(np.arange(8.1, 9.3001, 0.1), 50)
unique_magnitudes = np.unique(magnitudes)
lengths = 10 ** np.random.normal(0.1, 1, size=len(magnitudes)).cumsum()
widths = 10 ** np.random.normal(0.1, 1, size=len(magnitudes)).cumsum()
df = pd.DataFrame({'Magnitudes': magnitudes, 'Lengths': np.log10(lengths), 'Widths': np.log10(widths)})
# Colors for violins
palette = sns.color_palette("Set2", len(unique_magnitudes))
sns.set_style("darkgrid", {"grid.color": ".6", "grid.linestyle": ":"})
fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(10, 7))
sns.regplot(x='Magnitudes', y='Lengths', data=df, scatter_kws={"alpha": 0.0}, ax=ax1)
sns.violinplot(x='Magnitudes', y='Lengths', data=df, palette=palette, alpha=0.5, hue='Magnitudes', legend=False,
native_scale=True, ax=ax1)
ax1.set_title('Violin Plot of Lengths vs Magnitude')
ax1.set_xticks(unique_magnitudes) # make sure each x-position is labeled
sns.regplot(x='Magnitudes', y='Widths', data=df, scatter_kws={"alpha": 0.0}, ax=ax2)
sns.violinplot(x='Magnitudes', y='Widths', data=df, palette=palette, alpha=0.5, hue='Magnitudes', legend=False,
native_scale=True, ax=ax2)
ax2.set_title('Violin Plot of Widths vs Magnitude')
ax2.set_xticks(unique_magnitudes)
plt.tight_layout() # spacing for titles and labels
plt.show()