Skip to content

Instantly share code, notes, and snippets.

@linvinus
Created March 26, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linvinus/78c86cbd4eec7e7e85fb to your computer and use it in GitHub Desktop.
Save linvinus/78c86cbd4eec7e7e85fb to your computer and use it in GitHub Desktop.
"""
Some people prefer to write more pythonic, object-oriented code
rather than use the pyplot interface to matplotlib. This example shows
you how.
Unless you are an application developer, I recommend using part of the
pyplot interface, particularly the figure, close, subplot, axes, and
show commands. These hide a lot of complexity from you that you don't
need to see in normal figure creation, like instantiating DPI
instances, managing the bounding boxes of the figure elements,
creating and reaslizing GUI windows and embedding figures in them.
If you are an application developer and want to embed matplotlib in
your application, follow the lead of examples/embedding_in_wx.py,
examples/embedding_in_gtk.py or examples/embedding_in_tk.py. In this
case you will want to control the creation of all your figures,
embedding them in application windows, etc.
If you are a web application developer, you may want to use the
example in webapp_demo.py, which shows how to use the backend agg
figure canvase directly, with none of the globals (current figure,
current axes) that are present in the pyplot interface. Note that
there is no reason why the pyplot interface won't work for web
application developers, however.
If you see an example in the examples dir written in pyplot interface,
and you want to emulate that using the true python method calls, there
is an easy mapping. Many of those examples use 'set' to control
figure properties. Here's how to map those commands onto instance
methods
The syntax of set is
plt.setp(object or sequence, somestring, attribute)
if called with an object, set calls
object.set_somestring(attribute)
if called with a sequence, set does
for object in sequence:
object.set_somestring(attribute)
So for your example, if a is your axes object, you can do
a.set_xticklabels([])
a.set_yticklabels([])
a.set_xticks([])
a.set_yticks([])
"""
from matplotlib.pyplot import figure, show
from numpy import arange, sin, pi,cos
from math import sqrt
#allow ctrl+c to terminate from console
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
def GetSec(eq):
return abs(int( qe / (pi/3.0))) % 6
t = arange(0.0, 8*2*pi, 0.001)
Qe=t+(pi*5/12.0)+pi
u_alpha_orig= [ cos(qe) for (i,qe) in enumerate(Qe)]
u_betta_orig= [ sin(qe) for (i,qe) in enumerate(Qe)]
#Reverse Clark transform equations:
iao=[(2.0/3.0)*u_alpha_orig[i] for (i,qe) in enumerate(Qe)]
ibo=[ -(1.0/3.0)*u_alpha_orig[i]+(1.0/sqrt(3))*u_betta_orig[i] for (i,qe) in enumerate(Qe)]
ico=[-(1.0/3.0)*u_alpha_orig[i]-(1.0/sqrt(3))*u_betta_orig[i] for (i,qe) in enumerate(Qe)]
#~ err= Qe #-((pi/3.0)*(Qe(pi/3.0)))
#~ sin_f=(4.0/3.0)*sin(err)
#~ sin_f = [ (4.0/3.0)*sin( (60*int( (i*180.0/pi) / 60.0 ))*pi/180 ) for i in Qe]
#~ sin_f = [ (4.0/3.0)*sin((pi/3.0)*int( i / (pi/3.0) ) - i ) for i in Qe]
#~ u_q = [ (4.0/3.0)*sin((pi/6.0)+((pi/3.0)*int( qe / (pi/3.0) ) - qe )) for qe in Qe]
u_qerr = [ (4.0/3.0)*sin((pi/6.0)+((pi/3.0)*int( qe / (pi/3.0) ) - qe )) for qe in Qe]
#~ cos_f = [ cos((pi/6.0)+((pi/3.0)*int( i / (pi/3.0) ) - i )) for i in Qe]
u_derr = [ (4.0/3.0)*cos((pi/6.0)+((pi/3.0)*int( qe / (pi/3.0) ) - qe )) for qe in Qe]
#~ sin_f = [ (4.0/3.0)*sin( i ) for i in Qe]
u_alpha_err = [ (u_derr[i]*cos(qe)-u_qerr[i]*sin(qe)) for (i,qe) in enumerate(Qe)]
u_betta_err = [ (u_derr[i]*sin(qe)+u_qerr[i]*cos(qe)) for (i,qe) in enumerate(Qe)]
u_alpha_compensated = [u_alpha_orig[i] + u_alpha_err[i] for (i,qe) in enumerate(Qe)]
u_betta_compensated = [u_betta_orig[i] + u_betta_err[i] for (i,qe) in enumerate(Qe)]
#Reverse Clark transform equations:
#~ iaerr=[(2.0/3.0)*u_alpha_err[i] for (i,qe) in enumerate(Qe)]
#~ iberr=[-(1.0/3.0)*u_alpha_err[i]+(1.0/sqrt(3))*u_betta_err[i] for (i,qe) in enumerate(Qe)]
#~ icerr=[-(1.0/3.0)*u_alpha_err[i]-(1.0/sqrt(3))*u_betta_err[i] for (i,qe) in enumerate(Qe)]
iaerr=[ 1 if (GetSec(qe) == 0 or GetSec(qe) == 5) else (-1 if (GetSec(qe) == 2 or GetSec(qe) == 3 ) else 0) for (i,qe) in enumerate(Qe)]
iberr=[ 1 if (GetSec(qe) == 1 or GetSec(qe) == 2) else (-1 if (GetSec(qe) == 4 or GetSec(qe) == 5 ) else 0) for (i,qe) in enumerate(Qe)]
icerr=[ 1 if (GetSec(qe) == 3 or GetSec(qe) == 4) else (-1 if (GetSec(qe) == 0 or GetSec(qe) == 1 ) else 0) for (i,qe) in enumerate(Qe)]
#~ iberr=[-(1.0/3.0)*u_alpha_err[i]+(1.0/sqrt(3))*u_betta_err[i] for (i,qe) in enumerate(Qe)]
#~ icerr=[-(1.0/3.0)*u_alpha_err[i]-(1.0/sqrt(3))*u_betta_err[i] for (i,qe) in enumerate(Qe)]
#Reverse Clark transform equations:
#~ ia=[4 + (2.0/3.0)*u_alpha_compensated[i] for (i,qe) in enumerate(Qe)]
#~ ib=[6 + -(1.0/3.0)*u_alpha_compensated[i]+(1.0/sqrt(3))*u_betta_compensated[i] for (i,qe) in enumerate(Qe)]
#~ ic=[8 + -(1.0/3.0)*u_alpha_compensated[i]-(1.0/sqrt(3))*u_betta_compensated[i] for (i,qe) in enumerate(Qe)]
fig = figure(1)
ax1 = fig.add_subplot(211)
#~ ax1.plot(t*180.0/pi, sin(Qe))
#~ ax1.plot(t*180.0/pi, u_alpha_compensated,label="u_alpha_compensated")
#~ ax1.plot(t*180.0/pi, cos(Qe))
#~ ax1.plot(t*180.0/pi, u_betta_compensated,label="u_alpha_compensated")
ax1.plot(t*180.0/pi, [10 + iaerr[i]*0.3+ iao[i] for (i,qe) in enumerate(Qe)],label="ia")
ax1.plot(t*180.0/pi, [6 + iberr[i]*0.3+ ibo[i] for (i,qe) in enumerate(Qe)],label="ib")
ax1.plot(t*180.0/pi, [2 + icerr[i]*0.3+ ico[i] for (i,qe) in enumerate(Qe)],label="ic")
ax1.plot(t*180.0/pi, [-4 + iao[i] for (i,qe) in enumerate(Qe)],label="iao")
ax1.plot(t*180.0/pi, [-6 + ibo[i] for (i,qe) in enumerate(Qe)],label="ibo")
ax1.plot(t*180.0/pi, [-8 + ico[i] for (i,qe) in enumerate(Qe)],label="ico")
ax1.grid(True)
ax1.set_ylim((-10, 12))
ax1.set_ylabel('1 Hz')
ax1.set_title('current original and compensated')
ax1.legend(shadow=True, fancybox=True)
for label in ax1.get_xticklabels():
label.set_color('r')
ax2 = fig.add_subplot(212)
ax2.plot(t*180.0/pi, u_qerr, label="u_q_err" )
ax2.plot(t*180.0/pi, u_derr, label="u_d_err")
ax2.plot(t*180.0/pi, u_alpha_err, label="u_alpha_err")
ax2.plot(t*180.0/pi, u_betta_err, label="u_betta_err")
ax2.grid(True)
ax2.set_ylim((-2, 2))
ax2.legend(shadow=True, fancybox=True)
l = ax2.set_xlabel('deadtime compensation')
l.set_color('g')
l.set_fontsize('large')
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment