Adding Finishing Touches to the Broken Axis Plot
In this final step, we will add finishing touches to our broken axis plot to make it clear that the y-axis is broken. We will add diagonal lines between the subplots to indicate the break, and we will improve the overall appearance of the plot with proper labels and a grid.
Add Diagonal Break Lines
To visually indicate that the axis is broken, we will add diagonal lines between the two subplots. This is a common convention that helps viewers understand that some part of the axis has been omitted.
Create a new cell and add the following code:
## Create two subplots stacked vertically with shared x-axis
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))
## Plot the same data on both axes
ax1.plot(pts, 'o-', color='blue')
ax2.plot(pts, 'o-', color='blue')
## Set y-axis limits for each subplot
ax1.set_ylim(0.78, 1.0) ## Top subplot shows only the outliers
ax2.set_ylim(0, 0.22) ## Bottom subplot shows only the main data
## Hide the spines between ax1 and ax2
ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)
## Adjust the position of the ticks
ax1.xaxis.tick_top() ## Move x-axis ticks to the top
ax1.tick_params(labeltop=False) ## Hide x-axis tick labels at the top
ax2.xaxis.tick_bottom() ## Keep x-axis ticks at the bottom
## Add diagonal break lines
d = 0.5 ## proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
linestyle='none', color='k', mec='k', mew=1, clip_on=False)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)
## Add labels and a title
ax2.set_xlabel('Data Point Index')
ax2.set_ylabel('Value')
ax1.set_ylabel('Value')
fig.suptitle('Dataset with Outliers', fontsize=16)
## Add a grid to both subplots for better readability
ax1.grid(True, linestyle='--', alpha=0.7)
ax2.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.subplots_adjust(hspace=0.1) ## Adjust the space between subplots
plt.show()
When you run this cell, you should see the complete broken axis plot with diagonal lines indicating the break in the y-axis. The plot now has a title, axis labels, and grid lines to improve readability.
Understanding the Broken Axis Plot
Let's take a moment to understand the key components of our broken axis plot:
- Two Subplots: We created two separate subplots, each focusing on a different range of y-values.
- Hidden Spines: We hid the connecting spines between the subplots to create a visual separation.
- Diagonal Break Lines: We added diagonal lines to indicate that the axis is broken.
- Y-Axis Limits: We set different y-axis limits for each subplot to focus on specific parts of the data.
- Grid Lines: We added grid lines to improve readability and make it easier to estimate values.
This technique is especially useful when you have outliers in your data that would otherwise compress the visualization of the majority of your data points. By "breaking" the axis, you can show both the outliers and the main data distribution clearly in a single figure.
Experiment with the Plot
Now that you understand how to create a broken axis plot, you can experiment with different configurations. Try changing the y-axis limits, adding more features to the plot, or applying this technique to your own data.
For example, you can modify the previous code to include a legend, change the color scheme, or adjust the marker styles:
## Create two subplots stacked vertically with shared x-axis
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))
## Plot the same data on both axes with different styles
ax1.plot(pts, 'o-', color='darkblue', label='Data Points', linewidth=2)
ax2.plot(pts, 'o-', color='darkblue', linewidth=2)
## Mark the outliers with a different color
outlier_indices = [3, 14]
ax1.plot(outlier_indices, pts[outlier_indices], 'ro', markersize=8, label='Outliers')
## Set y-axis limits for each subplot
ax1.set_ylim(0.78, 1.0) ## Top subplot shows only the outliers
ax2.set_ylim(0, 0.22) ## Bottom subplot shows only the main data
## Hide the spines between ax1 and ax2
ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)
## Adjust the position of the ticks
ax1.xaxis.tick_top() ## Move x-axis ticks to the top
ax1.tick_params(labeltop=False) ## Hide x-axis tick labels at the top
ax2.xaxis.tick_bottom() ## Keep x-axis ticks at the bottom
## Add diagonal break lines
d = 0.5 ## proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
linestyle='none', color='k', mec='k', mew=1, clip_on=False)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)
## Add labels and a title
ax2.set_xlabel('Data Point Index')
ax2.set_ylabel('Value')
ax1.set_ylabel('Value')
fig.suptitle('Dataset with Outliers - Enhanced Visualization', fontsize=16)
## Add a grid to both subplots for better readability
ax1.grid(True, linestyle='--', alpha=0.7)
ax2.grid(True, linestyle='--', alpha=0.7)
## Add a legend to the top subplot
ax1.legend(loc='upper right')
plt.tight_layout()
plt.subplots_adjust(hspace=0.1) ## Adjust the space between subplots
plt.show()
When you run this enhanced code, you should see an improved visualization with outliers specifically marked and a legend explaining the data points.
Congratulations! You have successfully created a broken axis plot in Python using Matplotlib. This technique will help you create more effective visualizations when dealing with data that contains outliers.