Skip to content

Instantly share code, notes, and snippets.

@cdeitrick
Last active May 21, 2025 15:39
Show Gist options
  • Select an option

  • Save cdeitrick/a4d55a40e24cb9a03cbb084d313e8a0e to your computer and use it in GitHub Desktop.

Select an option

Save cdeitrick/a4d55a40e24cb9a03cbb084d313e8a0e to your computer and use it in GitHub Desktop.
Matplotlib Snippets
#Add padding to a figure after the plot is generated
plt.gcf().subplots_adjust(left = 0.30)
########################### Change grid spacing ################################
import numpy as np
import matplotlib.pyplot as plt
fig,axes=plt.subplots(nrows=2,ncols=1,figsize=(8,6))
########################### Change tick labels ################################
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = 'Testing'
ax.set_xticklabels(labels)
# Alternative version
major_ticks_top=np.linspace(0,20,6)
minor_ticks_top=np.linspace(0,20,21)
major_ticks_bottom=np.linspace(0,15,6)
axes[0].set_xticks(major_ticks_top)
axes[0].set_yticks(major_ticks_top)
axes[0].set_xticks(minor_ticks_top,minor=True)
axes[0].set_yticks(minor_ticks_top,minor=True)
axes[0].set_title("Subplot 1")
axes[0].grid(which="major",alpha=0.6)
axes[0].grid(which="minor",alpha=0.3)
axes[1].set_xticks(major_ticks_bottom)
axes[1].set_yticks(major_ticks_bottom)
axes[1].set_title("Subplot 2")
axes[1].grid()
plt.tight_layout()
plt.show()
###################### Change ticklabel size ##########################
plt.yticks(fontsize=20)
ax.tick_params(axis='x', labelsize=20)
######################## Remove tick labels ###########################
ax.xaxis.set_visible(True)
ax.axis('off')
ax.set_axis_off()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
################# rotate x-axis tick labels ##################
plt.xticks(rotation=45)
ax.tick_params(axis='x', labelrotation=90)
################# rotate y-axis tick labels ##################
plt.yticks(rotation=90)
################ Remove the legend from a plot ###############
ax.get_legend().remove()
############ Change the color of a single tick label ##########
import matplotlib.pyplot as plt
tick_labels = ax.yaxis.get_major_ticks()
for index, tick in enumerate(tick_labels):
text = tick.label1.get_text()
tick.label1.set_color(color)
############ Change the color of the plot background ###################
# Set background color of the figure
fig.patch.set_facecolor(color_background)
# Set background color of the subplot
ax.set_facecolor(color_background)
# `savefig` overrides the facecolor for the background of the figure
# You need to supply the 'facecolor' argument to `savefig` manually
################ Change color of the plot frame ###########################
ax.spines['bottom'].set_color(color_background)
ax.spines['top'].set_color(color_background)
ax.spines['left'].set_color(color_background)
ax.spines['right'].set_color(color_background)
############################ Plot a Circle ################################
circle1 = plt.Circle((0, 0), 0.2, color='r')
circle2 = plt.Circle((0.5, 0.5), 0.2, color='blue')
circle3 = plt.Circle((1, 1), 0.2, color='g', clip_on=False)
fig, ax = plt.subplots() # note we must use plt.subplots, not plt.subplot
# (or if you have an existing figure)
# fig = plt.gcf()
# ax = fig.gca()
ax.add_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment