Skip to content

Instantly share code, notes, and snippets.

@geogradient
Last active August 29, 2015 14:20
Show Gist options
  • Save geogradient/93ba99077fdfe16d211e to your computer and use it in GitHub Desktop.
Save geogradient/93ba99077fdfe16d211e to your computer and use it in GitHub Desktop.
Elements that can be useful in matplotlib. These are not secuential, just a copy&paste of bits and bytes of code used while learning matplotlib. Mostly for me to remember how I solved.
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
%matplotlib inline
#
fig = plt.figure()
ax = fig.add_subplot(111)
# or
fig = plt.figure(figsize=(10, 20)) #figsize=(16, 8), figsize=figsize, frameon=False
ax = plt.gca()
#ax.axis('off')
fig.patch.set_facecolor('white')
ax1 = plt.subplot2grid((3,2), (0, 0), colspan=2)
#
ax1.set_title(station + " : " +str(year), fontsize=24)
ax1.axesPatch.set_facecolor("black")
plt.grid(True, color="w")
#
# Grids
# set your ticks manually
ax.xaxis.set_ticks([1.,2.,3.,10.])
# Adding text to identify the figure
ax1.text(0.95, 0.85, "insitu",
horizontalalignment='right',
fontsize=20,
transform = ax1.transAxes, color = colours_experiment[year])
# Legends - note here that scatterpoints = 1 is only valid for scatterplots,
# for other point style figures use numpoints instead.
ax1.legend(scatterpoints = 1, loc="upper left",bbox_to_anchor=(1.10, 0.9), frameon = False)
#
ax1.set_ylim((0,10))
ax1.set_xlabel("week", fontsize=14)
ax1.set_ylabel("SPM [mg m-3]", fontsize=14)
#
plt.setp(ax1.get_xticklabels(), visible=True)
# stacked bar plot
ax2.bar(X, df2['PRYM'], color = colours_taxa['PRYM'], width = 1, label = 'PRYM',
bottom= df2['CRYP'] + df2['CYAN'] + df2['DIAT'] + df2['DINO'] + df2['MESO'] + df2['PRAS'])
##
#################### example with carbon ##############
ax3 = plt.subplot2grid((3,2), (2,0), colspan=2)
df3 = rel_carbon[station][year].T
X = np.array(df3.index) - 0.5
plt.bar(X, df3['CRYP'], color = colours_taxa['CRYP'], width = 1, label = 'CRYP',
bottom = None)
plt.bar(X, df3['CYAN'], color = colours_taxa['CYAN'], width = 1, label = 'CYAN',
bottom= df3['CRYP'])
plt.bar(X, df3['DIAT'], color = colours_taxa['DIAT'], width = 1, label = 'DIAT',
bottom= df3['CRYP'] + df3['CYAN'])
plt.bar(X, df3['DINO'], color = colours_taxa['DINO'], width = 1, label = 'DINO',
bottom= df3['CRYP'] + df3['CYAN'] + df3['DIAT'])
plt.bar(X, df3['MESO'], color = colours_taxa['MESO'], width = 1, label = 'MESO',
bottom= df3['CRYP'] + df3['CYAN'] + df3['DIAT'] + df3['DINO'])
plt.bar(X, df3['PRAS'], color = colours_taxa['PRAS'], width = 1, label = 'PRAS',
bottom= df3['CRYP'] + df3['CYAN'] + df3['DIAT'] + df3['DINO'] + df3['MESO'])
plt.bar(X, df3['PRYM'], color = colours_taxa['PRYM'], width = 1, label = 'PRYM',
bottom= df3['CRYP'] + df3['CYAN'] + df3['DIAT'] + df3['DINO'] + df3['MESO'] + df3['PRAS'])
sb = plt.bar(X, df3['others'], color = colours_taxa['others'], width = 1, label = 'others',
bottom= df3['CRYP'] + df3['CYAN'] + df3['DIAT'] + df3['DINO'] + df3['MESO'] + df3['PRAS'] + df3['PRYM'])
plt.legend(loc="upper left",bbox_to_anchor=(-0.15, -0.05), ncol= 8)
ax3.set_ylim((0,100))
ax3.set_xlabel("week", fontsize=14)
ax3.set_ylabel("Relative carbon content [%]", fontsize=14)
ax3.legend = False
axtwin3 = ax3.twinx()
xh=totals[station]["total_carbon_ugL"][year].index
yh=totals[station]["total_carbon_ugL"][year].get_values()
h = plt.scatter(xh, yh, label="total_carbon", marker=(12,0), c='black', s = 50)
axtwin3.set_ylabel("sumMeCarbProv [ugL]", fontsize=14)
fig.subplots_adjust(hspace=0.05)
# Hidden the sharedx labels
for ax in [ax1]:
plt.setp(ax.get_xticklabels(), visible=True)
# The y-ticks will overlap with "hspace=0", so we'll hide the bottom tick
#ax.set_yticks(ax.get_yticks()[1:])
##########################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment