Figure size in Matplotlib

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
schneitzmaster
User
Beiträge: 94
Registriert: Freitag 26. Oktober 2012, 15:35
Wohnort: Hamburg

Hallo ich möchte in matplotlib ein Diagramm darstellen und die Größe des Diagramms inklusive Legende genau festsetzen.
Ich möchte weiterhin die Legende mit bbox_to_anchor individuell positionieren. Leider wird dann nicht mehr die spezifierte figure size eingehalten.
Hat jemand eine Idee wie ich das umgehen kann?

Hier mein Beispiel:

Code: Alles auswählen

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import matplotlib
import pylab

def set_fig_size(scale=1.0):
    fig_width_pt  = 469.79135                  # Get this from LaTeX using \the\textwidth
    inches_per_pt = 1.0/72.27                  # Convert pt to inch
    golden_mean   = (np.sqrt(5.0)-1.0)/2.0     # Aesthetic ratio (you could change this)
    fig_width     = fig_width_pt*inches_per_pt # width in inches
    fig_height    = fig_width*golden_mean      # height in inches
    fig_size      = np.array([fig_width,fig_height])
    fig_size      = scale*fig_size
    print 'fig_size = ',fig_size
    params = {'text.usetex'    : True,
              'backend'        :'ps',
              "font.family": "serif",
              "font.serif": [],                   # blank entries should cause plots to inherit fonts from the document
              "font.monospace": [],          
              'axes.labelsize' : 11,
              'text.fontsize'  : 11,
              'legend.fontsize': 11,
              'xtick.labelsize': 11,
              'ytick.labelsize': 11,              
              'figure.figsize' : fig_size,
              'axes.linewidth' : 0.5
              }
    plt.rcParams.update(params)
    return plt

def disp_plt():
    wm = plt.get_current_fig_manager()
    wm.window.wm_geometry("+0+0")
    plt.tight_layout() 
    plt.show(block=False)       
    return
if __name__ == "__main__":   
    l_width      = 1.5
    marker_size  = 10    
    l_width      = 2
    ecolor       = 'grey'
    elinewidth   = 1.5    
    bar_w        = 1   
    lw_err       = 1
    capsize_err  = 2
    capthick_err = 1    
    transparent_flag = True    

    # PLOT SINUS
    t     = np.arange(0.0, 1.0, 0.01)
    sin_t = np.sin(2.*np.pi*t)
    cos_t = np.cos(2.*np.pi*t)
    
    plt     = set_fig_size(scale=1.0)
    fig, ax = plt.subplots()    
    
    
    p1 = ax.plot(t, sin_t,marker='.')
    p2 = ax.plot(t, cos_t,'r--',marker='o')
    
    ax.set_xticks([0,0.5,1.])
    ax.set_xticklabels(['t=0.0','t=0.5','t=1.0'],
                       rotation=30, ha='right',fontsize = 9)    
    ax.set_xlabel(r'$t [s]$')    
    ax.set_ylabel(r'$\sin \left( 2\pi \right) [-]$')         


    leg = ax.legend(['sine','cosine'],
                    numpoints=2,
                    bbox_to_anchor=(1.05, 1.01),
                    loc=2,          
                    ##ncol=5,
                    frameon=False
                    )

    ext = 'eps'
    plt.savefig('SINUS'+'.'+ext,
                bbox_inches='tight',format=ext,dpi=300,
                #transparent=transparent_flag
                )
    disp_plt()
Zuletzt geändert von Anonymous am Donnerstag 9. Juni 2016, 14:38, insgesamt 1-mal geändert.
Grund: Quelltext in Python-Codebox-Tags gesetzt.
Antworten