#!/usr/bin/python # 2016 )c( by Tosten Lehmann # This is a little script demonstrating how to plot to a pdf file # using matplotlib (or pylab). # Shows a few of common things to do with plots to get you started. # With matplotlib, the possibilities are (almost) endless... # Import all plotting and common matlab functionality # (purists avert your eyes!). from pylab import * # Make sure to use type 1 (postscript) fonts rcParams['ps.useafm']=True rcParams['pdf.use14corefonts']=True rcParams['font.family']='serif' rcParams['font.serif']='Times' rcParams['font.size']=10.0 # (pt) need to align with the figure size rcParams['text.usetex']=True # needed for '-' sign # Generate some data seed(0) N=200 timev=linspace(0,1,N) sigv=sin(4*pi*timev)+(rand(N)-0.5)*0.5 # I find this size (5.5" by 3.5") good for including in documents fig=figure(figsize=(5.5,3.5)) # In case you want the figure to go closer to the page margin than default # and to adjust white space between subplots depending on label needs subplots_adjust(left=0.11,bottom=0.11,right=0.89,top=0.97,wspace=0.3,hspace=0.2) # Some common plotting functions # (matlab-like commands should be self-explanatory) subplot(211) plot(timev,sigv**2,'ro') plot(timev,sigv) ylabel('Value [V or W]') legend(['squared','signal']) grid() # ... and a histogram in another subplot. ax=subplot(223) #provide axes handle for annotation hist(sigv,bins=30) xlim(-1.3,1.3) ylabel('Count') xlabel('Amplitude [V]') ax.annotate('Label',xy=(0.5,8),xytext=(-0.5,10),arrowprops=dict(facecolor='black',frac=0.2,width=0.2)) ax1=subplot(224) xlabel('Time [s]') # need to appear before twinx() grid() # need to appear before twinx() for y-grid aligned with left axes plot(timev,sigv**2,'ro') ylabel('Squared [W]') ax2=ax1.twinx() # add another y-axes on the right plot(timev,sigv) ylabel('Signal [V]') # File name ending determines type - I like to use pdf figures savefig('pylabplotout.pdf')