Seaborn lmplot配置求助:设置y轴显示分段范围
How to Create a Broken Y-Axis in Seaborn lmplot for Correlation Coefficients
Got it, let's fix this up so your y-axis only shows the extreme correlation ranges (-1.0 to -0.8 and 0.8 to 1.0) by breaking the middle section. Here's how to adjust your existing code to make this happen:
Modified Code
import seaborn as sns import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec # Your original lmplot setup (kept all your original parameters) g = sns.lmplot( x='Aantal Sterkst Negatief', # Horizontal axis y='Corr Sterkst Negatief', # Vertical axis data=df_PAR_metingen_half_1, # Data source fit_reg=False, # Don't fix a regression line hue="Sterkst Negatief", # Color by category scatter_kws={ "marker": "D", # Marker style "s": 100, 'alpha': 0.3 }, height=12 # Base figure size ) # Set up a broken y-axis using GridSpec to split the plot into two stacked axes fig = g.fig # Create 2 rows, 1 column; small gap between axes to show the break gs = GridSpec(2, 1, height_ratios=[1, 1], hspace=0.05) # Upper axis: displays 0.8 to 1.0 range ax1 = fig.add_subplot(gs[0]) # Lower axis: displays -1.0 to -0.8 range ax2 = fig.add_subplot(gs[1]) # Copy all scatter plot elements from the original lmplot axis to both new axes original_ax = g.axes[0,0] for line in original_ax.get_lines(): ax1.add_line(line) ax2.add_line(line) for collection in original_ax.collections: ax1.add_collection(collection) ax2.add_collection(collection) # Define y-axis limits for each segment ax1.set_ylim(0.8, 1.0) ax2.set_ylim(-1.0, -0.8) # Hide connecting spines to visually break the axis ax1.spines['bottom'].set_visible(False) ax2.spines['top'].set_visible(False) # Hide x-ticks on upper axis since they'll be duplicated ax1.tick_params(axis='x', bottom=False, labelbottom=False) # Add slanted break markers to clearly indicate the broken axis d = 0.015 # Size of the break lines kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False) ax1.plot((-d, +d), (-d, +d), **kwargs) ax1.plot((1-d, 1+d), (-d, +d), **kwargs) kwargs.update(transform=ax2.transAxes) ax2.plot((-d, +d), (1-d, 1+d), **kwargs) ax2.plot((1-d, 1+d), (1-d, 1+d), **kwargs) # Add your original plot styling elements # Horizontal red line at y=0 (it won't appear in either axis since our limits exclude 0) ax1.axhline(y=0, c='red', linestyle='dashed', zorder=-1) ax2.axhline(y=0, c='red', linestyle='dashed', zorder=-1) # Set title and labels to span both axes fig.suptitle('Correlations by chemical', fontweight='bold', fontsize=12, y=0.95) ax2.set_xlabel('Count') fig.text(0.01, 0.5, 'Correlation Coefficient', va='center', rotation='vertical') # Adjust layout to avoid overlapping elements plt.tight_layout() plt.show()
Quick Breakdown of Key Changes:
- We use
GridSpecto stack two axes with a tiny gap, creating the broken axis effect. - All your original scatter plot data is copied to both axes so points in the extreme ranges show up where they belong.
- We hide the connecting spines and add slanted lines to make the axis break obvious to viewers.
- Title and labels are adjusted to span both axes for a clean, professional look.
内容的提问来源于stack exchange,提问作者DataScience
相关产品推荐
相关产品推荐

