2012-10-15 8 views
5

Ich habe Probleme beim Ändern der Hilfsstrichbeschriftungseigenschaften in einem Diagramm mit Doppelachsen. Ich möchte, dass der Text klein und in einer anderen Schriftart als der Standard ist. Ich fand einen Weg, der gut funktionierte, bis ich es mit twiny() versuchte. Die zweite Achse reagiert nicht auf die Anweisungen zur Tick-Formatierung, wie Sie in der Abbildung sehen können. Fehle ich etwas Entscheidendes oder gibt es einen Fehler in Matplotlib?Anpassen der Hilfsstrichsgröße auf den Zwillingsachsen

Ubuntu 12.04, Python 2.7.3, matplotlib 1.1.1rc

#!/usr/bin/env python 
# coding: utf-8 

from matplotlib import pyplot as p 
from numpy import sin, cos, arange 

x = arange(0, 10, .01) 

for plotnum in range(1,5): 
    p.subplot(2, 2, plotnum) 
    ax1 = p.gca() 
    p.plot(sin(x),x) 
    ax2 = p.twiny() 
    p.plot(cos(x)+plotnum,x, 'g--') 

    # Set size all tick labels 
    # Works for first axes (lower x-ticks) and can also change font 
    for tickset in [ax1.xaxis.get_major_ticks()]: 
     [(tick.label.set_fontsize(plotnum*4), tick.label.set_fontname('ubuntu mono')) for tick in tickset] 

    # Does not work for second axes (upper x-ticks) 
    for tickset in [ax2.xaxis.get_major_ticks()]: 
     [(tick.label.set_fontsize(plotnum*2), tick.label.set_fontname('ubuntu mono')) for tick in tickset] 

    # This works, but doesn't allow changing font 
    #ax2.tick_params(axis='both', which='major', labelsize=plotnum*2) 

Hier ist ein Bild:

Here the image is

Edit: feste falsche Einrücken von tick-Wechsel Linien

Edit: Eingefügt Bild (Thriveth)

Antwort

5

Ticks können zwei Etiketten (label1 und label2), entsprechend der Tick Klasse Dokumentation:

  • 1 bezieht sich auf den Boden des Grundstückes für xticks und die links für yticks
  • 2 bezieht sich auf die Spitze des Plots für xticks und rechts für yticks

die label Attribut bezieht sich immer auf label1.

Sie Ihr Skript durch Ändern der ax2 Linien beheben:

for tickset in [ax2.xaxis.get_major_ticks()]: 
    [(tick.label2.set_fontsize(plotnum*2), tick.label2.set_fontname('ubuntu mono')) 

Die get_majorticklabels Funktionen funktionieren wird, wenn Sie ll‘brauchen label1 oder label2 und vereinfacht das Skript:

from matplotlib import pyplot as p 
from numpy import sin, cos, arange 

x = arange(0, 10, .01) 

for plotnum in range(1,5): 
    p.subplot(2, 2, plotnum) 
    ax1 = p.gca() 
    p.plot(sin(x),x) 
    ax2 = p.twiny() 
    p.plot(cos(x)+plotnum,x, 'g--') 
    for label in ax1.xaxis.get_majorticklabels(): 
     label.set_fontsize(plotnum * 4) 
     label.set_fontname('courier') 
    for label in ax2.xaxis.get_majorticklabels(): 
     label.set_fontsize(plotnum * 4) 
     label.set_fontname('verdana') 

Hinweis Ich setze die Etikettenwechselroutinen in die Schleife! plotted tick labels