xy - Diagramme

Jens Wagner 07/2015

Download this notebook

In [1]:
import matplotlib.pyplot as plt
import numpy as np
import pylab as py 
%matplotlib inline 
In [2]:
x = np.arange(40) #Datensaetze erzeugen
y1 = np.sqrt(x)
y2 = 0.01*x**2
In [3]:
plt.plot(x, y1)
plt.show()
In [4]:
plt.plot(x, y1, marker='o')
plt.show()

#   Marker:
#   +  Plus Sign
#   .  Dot
#   o  Circle
#   *  Star
#   p  Pentagon
#   s  Square
#   x  X Character
#   D  Diamond
#   h  Hexagon
#   ^  Triangle '''
In [5]:
plt.plot(x, y1, marker='.', color='r')
plt.show()

#   color:
#   r  Red
#   b  Blue
#   g  Green
#   c  Cyan
#   m  Magenta
#   y  Yellow
#   k  Black
#   w  White 
In [6]:
plt.plot(x, y1, marker='+', linestyle='None', color='r')
plt.show()

#   linestyle:
#   –    Solid Line
#   --   Dashed Line
#   :    Dotted Line
#   -.   Dash-Dotted Line
#   None No Connecting Lines

Legende, Achsenbeschriftungen, Achseneinteilung, Diagramm speichern

In [7]:
plt.plot(x, y1, marker='+', linestyle='None', color='b',label='Wurzel')
plt.plot(x, y2, marker='.', linestyle='None', color='g',label='Quadrat')
plt.legend()
plt.xlabel('x / unit')
plt.ylabel('y / unit')
plt.title('Basic Functions')
plt.axis([0, 40, 0, 8.5]) #x_start, x_stop, y_start, y_stop
#pl.xlim(0, 40)  x-Bereich
#pl.ylim(0, 8.5) y-Bereich

#Diagramm speichern:
#Supported formats: emf, eps, pdf, png, jpg, ps, raw, rgba, svg, svgz
plt.savefig('figures/myfunction.pdf', format='PDF') 
plt.show()

Latexcode in die Legende einbinden

In [8]:
plt.plot(x, y1, marker='+', linestyle='None', color='b',label='$\sqrt{x}$')  #'$ mein Latex $'
plt.plot(x, y2, marker='.', linestyle='None', color='g',label='$0.01 x^2$')
plt.legend()
Out[8]:
<matplotlib.legend.Legend at 0x755fcf8>

Legende platzieren

In [9]:
plt.plot(x, y1, marker='+', linestyle='None', color='b',label='$\sqrt{x}$')  
plt.plot(x, y2, marker='.', linestyle='None', color='g',label='$0.01 x^2$')
plt.legend(loc=0)  #loc=0 links oben, #loc=1 (default) rechts oben, #loc=2 rechts unten, #loc=3 links unten
Out[9]:
<matplotlib.legend.Legend at 0x7405b70>
In [10]:
plt.plot(x, y1, marker='+', linestyle='None', color='b',label='$\sqrt{x}$')  
plt.plot(x, y2, marker='.', linestyle='None', color='g',label='$0.01 x^2$')
plt.legend(loc='best') #findet die beste Position für die Legende
Out[10]:
<matplotlib.legend.Legend at 0x733e9e8>
In [11]:
plt.plot(x, y1, marker='+', linestyle='None', color='b',label='$\sqrt{x}$')  
plt.plot(x, y2, marker='.', linestyle='None', color='g',label='$0.01 x^2$')
plt.legend(bbox_to_anchor=(1.28, 1.02))  # Legende frei platzieren: bbox_to_anchor=(x,y)
Out[11]:
<matplotlib.legend.Legend at 0x735b780>

Groessen anpassen

In [12]:
plt.figure(figsize=(8,6)) #Diagrammgroesse
plt.plot(x, y1, marker='o',markersize=5, linestyle='None', color='b',label=r'$\sqrt{x}$')  #Markergroesse: markersize=
plt.plot(x, y2, marker='*',markersize=8, linestyle='None', color='g',label=r'$\frac{x^2}{100}$')
plt.legend(loc='best',prop={'size':20}) #Legendengroesse
Out[12]:
<matplotlib.legend.Legend at 0x763d9e8>

Customizing matplotlib

The matplotlibrc file

http://matplotlib.org/users/customizing.html

In [13]:
plt.rc('figure', figsize=(9,6))
plt.rc('axes', grid=True,labelsize=16) 
plt.rc('xtick', labelsize=16) 
plt.rc('ytick', labelsize=16) 
plt.rc('lines', markersize=8, linestyle='None') 
plt.rc('legend',markerscale=1.5, loc='best', fontsize=20, shadow=True, framealpha=1) 

plt.plot(x, y1, marker='+', color='b',label='$\sqrt{x}$')  
plt.plot(x, y2, marker='*', color='g',label='$0.01 x^2$')
plt.xlabel('x / unit')
plt.ylabel('y / unit')
plt.legend()  
Out[13]:
<matplotlib.legend.Legend at 0x86c3ef0>
In [14]:
plt.rcdefaults()   #Formatierung auf default zuruecksetzen
%matplotlib inline 

Logarithmische Skalierung

Download zerfall.dat

In [15]:
t, N = py.loadtxt('data/zerfall.dat', unpack=True) #Messwerte importieren

plt.plot(t,N,linestyle='None',marker='o')
plt.xlabel('Zeit / min')
plt.ylabel('counts / N')
plt.title('Radioaktiver Zerfall')

plt.yscale('log')

Fehlerbalken

In [16]:
y_err=np.sqrt(N)
plt.errorbar(t, N, yerr=y_err, fmt='.')  #plt.errorbar(x, y, yerr=None, xerr=None, fmt='',...
plt.xlabel('Zeit / min')
plt.ylabel('counts / N')
plt.title('Radioaktiver Zerfall')
plt.yscale('log')