"""
Figura multipla
a. grafico con tre frames
b. aggiunta linee di riferimento
c. labels con simboli
d. manipolazione legend
e. aggiustamento frames
f. annotazione
g. aggiunta frame con zoom
h. adeguamento standard per pubblicazione
"""

import numpy as np              
import matplotlib.pyplot as plt 
import ftu                        

"""
# h. adeguamento standard pubblicazione
import ftuplot
ftuplot.jetstandard() 
# vedi FTUplotPython.pptx (da WIP_2014_10_21) per altri usi di ftuplot
"""

mhdcoil = 26                                             # scelta del sensore magnetico veloce

ns1 = 40225
Ip1 = ftu.ftudata(ns1, 'ZZZZED.IPL')
q1 = ftu.ftudata(ns1, '$EQEQPSI')
MHD1 = ftu.ftudata(ns1, 'MHDFST.CH{}'.format(mhdcoil))   # molto utile per ciclare su coils diversi

ns2 = 40217
Ip2 = ftu.ftudata(ns2, 'ZZZZED.IPL')
q2 = ftu.ftudata(ns2, '$EQEQPSI')
MHD2 = ftu.ftudata(ns2, 'MHDFST.CH{}'.format(mhdcoil))

# a. grafico con tre frames

plt.figure(facecolor='w')
#plt.subplots_adjust(hspace=0.001)                                  # e. aggiusta distanze
h1 = plt.subplot(311)                                           # tre righe, una colonna, primo plot
plt.plot(Ip1.t, 1.e-6*Ip1.v, label='pulse '+str(ns1))           # usati dati .t e .v invece del metodo plot
h1.plot(Ip2.t, 1.e-6*Ip2.v, color='r', label='pulse '+str(ns2)) # notare h1 al posto di plt
plt.ylabel('Plasma current (MA)')
plt.title('Esercizio3')     
plt.legend()                                                    # mostra le labels definite nelle plot
#plt.legend(loc=2, prop={'size':8})                                 # d. manipolazione legend
#plt.setp(h1.get_xticklabels(), visible=False)                      # e. elimina labels inutili

h2 = plt.subplot(312, sharex=h1)                                # secondo plot con x agganciata ad h1
h2.plot(q1.t, q1.v, label='pulse '+str(ns1))                  
h2.plot(q2.t, q2.v, color='r', label='pulse '+str(ns2))        
#h2.axhline(3.0, linestyle='--', color='k')                         # b. linea orizz. q_psi=3 tratteggiata
plt.ylabel('q_psi')  
#plt.ylabel(r'$q_{\psi}$', fontsize=16)                             # c. label con TeX interpreter
#plt.setp(h2.get_xticklabels(), visible=False)                      # e. elimina labels inutili

h3 = plt.subplot(313, sharex=h1)                                # terzo plot con x agganciata ad h1
h3.plot(MHD1.t, MHD1.v, label='pulse '+str(ns1))                  
h3.plot(MHD2.t, MHD2.v, color='r', label='pulse '+str(ns2))        
plt.xlim([0.0, 1.3])                                            # limiti sulla x
#plt.annotate('Locked mode', xy=(0.19,0.0), xytext=(0.25,-20.0), 
    #arrowprops=dict(arrowstyle="->"))                               # f. annotazione con testo e freccia

plt.xlabel('Time (s)')

"""
# g. aggiunta frame con metodo plt.axes
zoombox = [0.5, 0.5, 0.3, 0.2]   # location of the zoom box in units of figure fraction
zoomt = [1.2, 1.22]              # time range to zoom
hzoom = plt.axes(zoombox)        # draw the box nd create object
hzoom.plot(MHD1.t, MHD1.v)       # plot in the box
plt.xlim(zoomt)                  # zoom time

# line connectors from box corners (in fig. fract.) to full signal (in data units)
h3.annotate("", xy=(zoombox[0], zoombox[1]), xycoords='figure fraction', xytext=(zoomt[0], zoomt[1]), 
    textcoords='data', arrowprops=dict(arrowstyle="-"))

"""
plt.show()

""" 
aggiungere a) ecc.
aggiustare zoom box
usare in legend: bbox_to_anchor, ncol, mode
"""
