# In iPython or the iPython notebook, it's easiest to use the pylab magic, which # imports matplotlib, numpy, and scipy. # The inline flag means that images will be shown here in the notebooks, rather # than in pop-up windows. %pylab inline # If you are using 'regular' Python, however, you'll want the following. You'll # need to also separately import numpy and any other packages that you might need. #import matplotlib.pyplot as plt # Make some data to plot x = np.linspace(0, 2*np.pi) y1 = np.sin(x) y2 = np.cos(x) # First, create an empty figure with 2 subplots # - The arguments (1, 2) indicate 1 row and 2 cols # - The function plt.subplots returns an object for the figure and for each axes # - There are multiple ways to accomplish this same goal, but this is probably the # simplest - notice that each subplot is associated with one of the axes objects. fig, (ax1, ax2) = plt.subplots(1, 2) # Next, put one line on the first axis and both lines on the second axis # - On the second axes, add a legend to distinguish the two lines ax1.plot(x, y1) ax2.plot(x, y1, label='sin') # The labels are what appear in the legend ax2.plot(x, y2, label='cos') ax2.legend() # Finally, save the figure as a png file fig.savefig('myfig.png') #ax1.plot? # Make some data to plot x = np.arange(0, 100) y = np.random.rand(100) # 100 random numbers # Make a figure with 6 subplots and axes fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3, 2) # Add data to each axis. Optional arguments to each method will customize each plot. ax1.plot(x, y) ax2.hist(y) ax3.scatter(x, y) ax4.boxplot(y) ax5.loglog(x, y) ax6.semilogx(x, y) # Read an image file for first subplot, generate random array for second img1 = plt.imread('lena.png') img2 = np.random.rand(128, 128) # Make figure fig, (ax1, ax2) = plt.subplots(1, 2) ax1.imshow(img1) ax2.imshow(img2) ax1.set_axis_off() # Hide "spines" on first axis # Try it here... %loadpy http://matplotlib.org/mpl_examples/pylab_examples/contour_demo.py # Make some data to plot x = np.linspace(0, 2*np.pi) y1 = np.sin(x) y2 = np.cos(x) # First, create an empty figure with 1 subplot fig, ax1 = plt.subplots(1, 1) # Add title and labels ax1.set_title('My Plot') ax1.set_xlabel('x') ax1.set_ylabel('y') # Change axis limits ax1.set_xlim([0,2]) ax1.set_ylim([-1, 2]) # Add the lines, changing their color, style, and marker ax1.plot(x, y1, 'k--o', label='sin') # Black line, dashed, with 'o' markers ax1.plot(x, y2, 'r-^', label='cos') # Red line, solid, with triangle-up markers # Adjust tick marks and get rid of 'box' ax1.tick_params(direction='out', top=False, right=False) # Turn ticks out ax1.spines['top'].set_visible(False) # Get rid of top axis line ax1.spines['right'].set_visible(False) # Get rid of bottom axis line # Add subplot letter ax1.annotate('(a)', (0.01, 0.96), size=12, xycoords='figure fraction') # Add legend ax1.legend() # Finally, save the figure as a png file fig.savefig('myfig-formatted.png') # View rcParams matplotlib.rcParams # Save default rcParams so we can reset them later # WARNING: Do not run this cell after changing rcParams, as it will overwrite the # defaults that we are trying to preserve. rcdef = plt.rcParams.copy() # Make sure rcParams is at default settings, since we're messing with it plt.rcParams.update(rcdef) # Make a simple figure with default formatting fig, axall = plt.subplots(1, 2) # axall is the tuple containing both axis objects for ax in axall: ax.plot(np.random.rand(100), 'k-o', label='Random') ax.set_ylim([0, 1.2]) ax.set_ylabel('Value') ax.legend() # Choose a bunch of new parameter values # In practice, you'll try modifying these, running the code and saving the figure, # looking at the figure, then making more modifications until you're happy. newparams = {'axes.labelsize': 14, 'axes.linewidth': 1, 'savefig.dpi': 300, 'lines.linewidth': 1.5, 'figure.figsize': (8, 3), 'figure.subplot.wspace': 0.4, 'ytick.labelsize': 12, 'xtick.labelsize': 12, 'ytick.major.pad': 5, 'xtick.major.pad': 5, 'legend.fontsize': 12, 'legend.frameon': False, 'legend.handlelength': 1.5} # Update the global rcParams dictionary with the new parameter choices # Before doing this, we reset rcParams to its default again, just in case plt.rcParams.update(rcdef) plt.rcParams.update(newparams) # Make the new figure with new formatting fig, axall = plt.subplots(1, 2) for ax in axall: ax.plot(np.random.rand(100), 'k-o', label='Random') ax.set_ylim([0, 1.2]) ax.set_ylabel('Value') ax.legend() fig.savefig('myfig-advanced.png') # Put rcParams back to default plt.rcParams.update(rcdef)