IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

Matplotlib avec deux axes Y [Python 3.X]


Sujet :

Tkinter Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    manutentionnaire
    Inscrit en
    Décembre 2020
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : manutentionnaire

    Informations forums :
    Inscription : Décembre 2020
    Messages : 31
    Par défaut Matplotlib avec deux axes Y
    j'ai implémenté un graphique dans un canvas.
    j'ai trouvé des exemples de graphique avec deux axes Y, mais avec la méthode plt.show().
    étant novice, comment puis intégrer un deuxième le deuxième axe Y, dans le canvas?
    merci pour votre aide

    Nom : _34.jpg
Affichages : 702
Taille : 81,3 Ko

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
    # coding:utf-8
    #version 3.x python
     
    from tkinter import *
    import tkinter as tk
    from tkinter import ttk
    print("TkVersion", TkVersion)
    print("TclVersion", TclVersion)
    print("Python version", sys.version_info)
    import sys; print('Python %s on %s' % (sys.version, sys.platform))
     
     
    from pylab import *
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from mpl_toolkits.axes_grid1.anchord_artists import AnchoredAuxTransformBox
    print("MatPlotLib version : ", matplotlib.__version__)
    print('MatPlotLib version : {}'.format(matplotlib.__version__))
     
    def _on_tab_changed(event):
    	print("\n")
     
    	nb = event.widget
    	nb.update_idletasks()
     
    	tab = nb.nametowidget(nb.select())
     
    	nb.configure(height=tab.winfo_reqheight())
    	nb.configure(width=tab.winfo_reqwidth())
     
     
    def Create_notebook(master=None, **kw):
    	nb = ttk.Notebook(master, **kw)
    	nb.bind("<<NotebookTabChanged>>", _on_tab_changed)
    	return nb
     
    root = tk.Tk()
    root.title("Titre")
    root.resizable(False, False)
     
    window_height = 800
    window_width = 900
     
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
     
    x_cordinate = int((screen_width/2) - (window_width/2))
    y_cordinate = int((screen_height/2) - (window_height/2))
     
    root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
     
    RightFrame = tk.LabelFrame(root, text="[Label]", font=('verdana', 8, ''), foreground="blue", relief=SOLID, borderwidth=0)
    RightFrame.place(x=130, y=5, width=900, height=900)
    RightFrame.anchor(anchor="center")
     
    notebook = Create_notebook(RightFrame)
    notebook.add(tk.Frame(notebook, bg="white", width=709, height=725, name="1"), text="GRAPH")
    notebook.place(x=5, y=5)
     
    Liste_TEST = [11.81, 4.91, 4.62, 4.19, 4.35, 4.22, 4.03, 4.31, 4.06, 4.23, 4.01, 4.28, 4.06, 3.81, 4.05, 4.04, 4.13, 4.08, 3.99, 4.05, 4.15, 4.26, 4.3, 4.35, 4.77, 4.65, 4.77, 4.52, 4.6]
     
    chiffre_inf = IntVar()
    chiffre_inf.set(floor(min(Liste_TEST)))
    chiffre_sup = IntVar()
    chiffre_sup.set(ceil(max(Liste_TEST)))
     
    fig = plt.Figure(figsize=(1, 1), dpi=96)
     
    ax = fig.add_subplot(1, 1, 1, frameon=True)
     
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
     
    ax.spines['bottom'].set_color('#000000')
     
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))
    ax.spines['left'].set_color('#000000')
     
    ax.tick_params(labelcolor='#000000', top=False, bottom=True, left=True, right=False)
     
    plt.xlim(1, len(Liste_TEST))
    plt.ylim(chiffre_inf.get(), chiffre_sup.get())
     
    major_ticks = np.arange(chiffre_inf.get(), chiffre_sup.get(), 1)
    minor_ticks = np.arange(chiffre_inf.get(), chiffre_sup.get(), 0.5)
    ax.set_yticks(major_ticks * 1)
    ax.set_yticks(minor_ticks * 1, minor=True)
    ax.grid(which='minor', alpha=0.2)
    ax.grid(which='major', alpha=0.5)
    ax.grid(True)
     
    ax.set_xlabel("Nombre de plein(s)", fontsize=6)
    ax.set_ylabel("Litre(s)", fontsize=6)
    ax.set_title("<" + "Titre tableau" + ">", fontsize=8, color='dimgrey')
     
    print("Liste_TEST >", Liste_TEST)
    ax.plot(range(len(Liste_TEST)), Liste_TEST, color="red", linewidth=.85, linestyle="dotted", label='Effective')
     
    graph = FigureCanvasTkAgg(fig, master=notebook.winfo_children()[0])
    canvas = graph.get_tk_widget()
    canvas.config(width=700, height=370, relief=RIDGE, borderwidth=2)
    canvas.place(x=2, y=50)
     
    root.mainloop()

  2. #2
    Membre averti
    Homme Profil pro
    manutentionnaire
    Inscrit en
    Décembre 2020
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : manutentionnaire

    Informations forums :
    Inscription : Décembre 2020
    Messages : 31
    Par défaut
    ceci fonctionne bien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
     
    # coding:utf-8
    #version 3.x python
     
    from tkinter import *
    import tkinter as tk
    from tkinter import ttk
    print("TkVersion", TkVersion)
    print("TclVersion", TclVersion)
    print("Python version", sys.version_info)
    import sys; print('Python %s on %s' % (sys.version, sys.platform))
     
     
    from pylab import *                                                                                         
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredAuxTransformBox
    print("MatPlotLib version : ", matplotlib.__version__)
    print('MatPlotLib version : {}'.format(matplotlib.__version__))
     
     
    def _on_tab_changed(event):
     
    	nb = event.widget
    	nb.update_idletasks()
     
    	tab = nb.nametowidget(nb.select())
     
    	nb.configure(height=tab.winfo_reqheight())												
    	nb.configure(width=tab.winfo_reqwidth())													    
     
     
    def Create_notebook(master=None, **kw):
    	nb = ttk.Notebook(master, **kw)
    	nb.bind("<<NotebookTabChanged>>", _on_tab_changed)
    	return nb
     
     
    root = tk.Tk()                                                                                                    
    root.title("Titre")
    root.resizable(False, False)                                                                                 
     
    window_height = 800
    window_width = 900
     
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
     
    x_cordinate = int((screen_width/2) - (window_width/2))
    y_cordinate = int((screen_height/2) - (window_height/2))
     
    root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
     
     
    RightFrame = tk.LabelFrame(root, text="[Label]", font=('verdana', 8, ''), foreground="blue", relief=SOLID, borderwidth=0)
    RightFrame.place(x=130, y=5, width=900, height=900)
    RightFrame.anchor(anchor="center")
     
     
    notebook = Create_notebook(RightFrame)
    notebook.add(tk.Frame(notebook, bg="white", width=709, height=725, name="1"), text="GRAPH")
    notebook.place(x=5, y=5)
     
     
    Liste_TEST = [11.81, 4.91, 4.62, 4.19, 4.35, 4.22, 4.03, 4.31, 4.06, 4.23, 4.01, 4.28, 4.06, 3.81, 4.05, 4.04, 4.13, 4.08, 3.99, 4.05, 4.15, 4.26, 4.3, 4.35, 4.77, 4.65, 4.77, 4.52, 4.6]
    y2 = [1.46,1.51,1.52,1.5,1.56,1.57,1.66,1.65,1.61,1.61,1.61,1.63,1.65,1.65,1.64,1.66,1.63,1.63,1.63,1.61,1.7,1.73,1.73,1.73,1.69,1.72,1.72,1.71,1.74]
     
    chiffre_inf = IntVar()
    chiffre_inf.set(floor(min(Liste_TEST)))                                                
    chiffre_sup = IntVar()
    chiffre_sup.set(ceil(max(Liste_TEST)))                                                
    print("chiffre_inf", chiffre_inf.get(), "chiffre_sup", chiffre_sup.get())
     
     
     
    fig = plt.Figure(figsize=(1, 1), dpi=96)
    ax1 = fig.add_subplot(1,1, 1, frameon=True, xlim=(0, len(Liste_TEST)), ylim=(chiffre_inf.get(), chiffre_sup.get()))
    ax2 = ax1.twinx()
     
    ax1.spines['bottom'].set_color('none')
    ax1.spines['top'].set_color('none')
    ax2.spines['right'].set_color('#e45cda')   															
    ax1.spines['left'].set_color('#FF0000')																	
     
    ax1.yaxis.set_ticks_position('left')
    ax1.spines['left'].set_position(('data', 0))
     
     
    ax1.tick_params(labelcolor='#FF0000', left=True)
    ax2.tick_params(labelcolor='#e45cda', right=True)
     
    ax1.plot(range(len(Liste_TEST)), Liste_TEST, color="red", linewidth=.85, linestyle="dotted", label='Effective')
    ax2.plot(range(len(Liste_TEST)), y2, color='#e45cda', linewidth=.85, linestyle="solid")
     
    graph = FigureCanvasTkAgg(fig, master=notebook.winfo_children()[0])
    canvas = graph.get_tk_widget()
    canvas.config(width=700, height=370, relief=RIDGE, borderwidth=2)
    canvas.place(x=2, y=50)
     
     
    root.mainloop()
    par contre, l'axe X n'est pas étendue sur toute la largeur. Cela provient de xlim
    Comment puis afficher, sur l'axe X les tranches (exemple entre 0 et 5 --> 0 1 2 3 4 5)?
    Comment indiquer une couleur des labels X --> ceci ne fonctionne pas --> ax.tick_params(labelcolor='#FF00FF', bottom=True)

    merci pour votre tempsNom : _35.jpg
Affichages : 569
Taille : 40,9 KoNom : _36.jpg
Affichages : 558
Taille : 34,0 Ko

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [JpGraph] histograme groupé avec deux axes Y
    Par Amelie MOREAU dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 18/04/2013, 15h22
  2. [XL-2007] Graphique linéaire avec deux axes "Y"
    Par Flaguette dans le forum Excel
    Réponses: 2
    Dernier message: 22/11/2012, 21h42
  3. Graphique avec deux axes différents
    Par loulip123 dans le forum MATLAB
    Réponses: 1
    Dernier message: 19/09/2011, 18h00
  4. Graphe avec deux Axes
    Par justgreat dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 12/08/2010, 16h19
  5. [Débutant] Graphe avec deux axes y différents
    Par Ricolaa dans le forum MATLAB
    Réponses: 1
    Dernier message: 12/10/2008, 17h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo