######################################################################################### # # Scientific Python Course # # L03 - Matplotlib # ######################################################################################### # # ARCHER, 2015 # # http://www.archer.ac.uk # support@archer.ac.uk # # L03 - Matplotlib # # Neelofer Banglawala : nbanglaw@epcc.ed.ac.uk # Kevin Stratford : kevin@epcc.ed.ac.uk # # EPCC, University of Ediburgh, EPSRC, NERC, CRAY # # # ******************************************************************** # * Reusing this material : * # * * # * This work is licensed under a Creative Commons Attribution- * # * Non-Commercial-ShareAlike 4.0 Internatiional License. * # * http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US * # * * # * This means you are free to copy and redistributhe the material * # * and adapt and build on the material under the following terms: * # * You must give appropriate credit, provide a link to the license * # * and indicate if changes were made. If you adapt or build on the * # * material you must distribute your work under the same license as * # * the original. * # * * # ******************************************************************** # # ######################################################################################### # # S6 [Matplotlib] Basic plotting # ######################################################################################### # # # add 'inline' option if using a notebook # %matplotlib inline # import matplotlib.pyplot as plt; import numpy as np # # # xmin=0; xmax=10; pts = 50; # x = np.linspace(xmin, xmax, pts); # y = np.cos(x); # # # line, markers, 2 plots, fig, title then plot # plt.plot(x,y,'ro'); # , x, y, 'g-' # # plt.show() # need this if in non-interactive mode # ######################################################################################### # # S7 [Matplotlib] Saving images to file # ######################################################################################### # # # save image to file in different formats # plt.savefig("cos_plot.pdf"); # plt.savefig("cos_plot.png", dpi=300); # higher resolution (dpi) # # ######################################################################################### # # S10 [Matplotlib] Plot customisations I # ######################################################################################### # # # Ex: set the figure size and add a plot # fig=plt.figure(figsize=(4,4)); # plt.plot(x,y,'c-') # # # Ex: linewidth, and # # linestyles: '-', '.-', ':', '--' # plt.plot(x,y,'k-',linewidth=2.0) # # # Ex: markers and their properties # # unfilled markers: '.',+','x','1' to '4','|' # plt.plot(x,y,'x',markersize=10) # # # filled markers: 'o', 's','*','d','>','^','v', 'p', 'h' # plt.plot(x,y,'8',markerfacecolor='None',markeredgecolor='g', # markersize=10) # # ######################################################################################### # # S11 [Matplotlib] Plot customisations II # ######################################################################################### # # # # Ex: x,y, axis limits: # plt.xlim((xmax*0.25,xmax*0.75)); # plt.ylim((np.cos(xmin*0.25),np.cos(xmax*0.75))); # plt.plot(x,y,'mo-') # # # Ex: title placement and font properties # plt.plot(x,y,'x') # plt.suptitle('A Centered Title', fontsize=20) # # loc: center, left, right # # verticalalignment: center, top, bottom, baseline # plt.title('A Placed Title', loc='left', verticalalignment='top' ) # ######################################################################################### # # S12 [Matplotlib] Plot customisations III # ######################################################################################### # # # # Ex: tick marks # fig=plt.figure(figsize=(4,3.5)); plt.plot(x,y,'x'); # nticks = 5; # tickpos = np.linspace(xmin,xmax,nticks); # labels = np.repeat(['tick'],nticks); # plt.xticks(tickpos, labels, rotation='vertical'); # # # Ex++: arrows and annotations # plt.plot(x,y,'x'); # atext='annotate this'; arrowtip=(1.5,0.5); textloc=(3, 0.75); # plt.annotate(atext, xy=arrowtip, xytext=textloc, # arrowprops=dict(facecolor='black', shrink=0.01),) # # ######################################################################################### # # S14 [Matplotlib] Subplots II # ######################################################################################### # # # (fig, axes) = plt.subplots(nrows=2, ncols=2); # axes.size # # axes[0,0].plot(x,y,'g-'); # axes[1,1].plot(x,y,'r-'); # # # # subplots_adjust(left=None, bottom=None, right=None, top=None, # # wspace=None, hspace=None) # plt.subplots_adjust(hspace=0.001) # # ######################################################################################### # # S15 [Matplotlib] Advanced : subplot2grid # ######################################################################################### # # # Ex++: subplot2grid(shape, loc, rowspan=1, colspan=1) # fig = plt.figure() # ax1 = plt.subplot2grid((3, 3), (0, 0)); ax1.plot(x,y,'r-'); # ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2); ax2.plot(x,y,'g-'); # ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2); ax3.plot(x,y,'b-'); # ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2); ax4.plot(x,y,'c-'); # # ######################################################################################### # # S18 [Matplotlib] Advanced : animation # ######################################################################################### # # from matplotlib import use # ## animation doesn't work with macosx backend! # use("nbagg") # #import earthquakes; # ######################################################################################### # # S20 [Matplotlib] matplotlibrc settings # ######################################################################################### # # from matplotlib import rc_file # # rc_file('/path/to/my/matplotlibrc') # # axes.labelsize : 9.0 # fontsize of the x any y labels # xtick.labelsize : 9.0 # fontsize of the tick labels # ytick.labelsize : 9.0 # fontsize of the tick labels # legend.fontsize : 9.0 # fontsize in legend # font.family : serif # font.serif : Computer Modern Roman # Marker size : lines.markersize : 3 # text.usetex : True # ######################################################################################### # # S21 [Matplotlib] Settings for a nice figure ratio # ######################################################################################### # # WIDTH = 500.0 # Figure width in pt (usually from LaTeX) # FACTOR = 0.45 # Fraction of the width you'd like the figure to use # widthpt = WIDTH FACTOR # inperpt = 1.0 / 72.27 # # use the Golden ratio because it looks good # golden_ratio = (np.sqrt(5) - 1.0) / 2.0 # widthin = widthpt inperpt # heightin = widthin * golden_ratio # figdims = [widthin, heightin] # Dimensions as list # fig = plt.figure(figsize=figdims) # ######################################################################################### # # S22 [Matplotlib] Include images in LaTeX # ######################################################################################### # # \begin{figure} # \includegraphics[width=0.45\textwidth]{figure.pdf} # \end{figure} # # ######################################################################################### # # END # #########################################################################################