2016-06-22 5 views
0

Ich habe ein Subplots Grid ohne Leerzeichen zwischen den Unterplots mit gemeinsamen x, y-Achsen erstellt. Ich zeige nur die Ticks und Labels für die äußeren Teilplots. Das Problem ist, dass sich die Tick-Nummern an den Grenzen der Subplots überschneiden. Mit MaxNLocator kann ich die oberen oder unteren Ticks entfernen, aber nur für alle Plots gleichzeitig.Entfernen Sie nur überlappende Ticks in Subplots Raster

Frage: Wie kann ich das höchste Häkchen nur für bestimmte Plots behalten (in diesem Fall x = 2.0 nur im unteren rechten Teilplot und y = 3 nur im oberen linken Teilplot)? Warum schlägt meine bedingte Einstellung von Ticks für bestimmte Subplots fehl?

enter image description here

Code:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import MaxNLocator 

numPlotsY = 3 
numPlotsX = 3 
f, ax_grid = plt.subplots(numPlotsY,numPlotsX,sharex=True,sharey=True) 

A = np.arange(numPlotsY)+1.0 
phi = np.arange(numPlotsX) 
x = np.linspace(0,2.0,100) 
fontsize = 18 

for y_i in range(0,numPlotsY): 
    for x_i in range(0,numPlotsX): 
     y = A[y_i]*np.sin(x*np.pi + phi[x_i]) 
     ax = ax_grid[y_i,x_i] 
     ax.plot(x,y,lw=2.0) 

     if x_i == 0: 
      ax.set_ylabel(r'$y$', fontsize=fontsize) 

     if y_i == numPlotsY-1: 
      ########################### 
      # Why doesn't this work?! # 
      ########################### 
      if x_i != numPlotsX-1: 
       nbins = len(ax.get_xticklabels()) 
       ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) 
      else: 
       nbins = len(ax.get_xticklabels()) 
       ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune=None)) 
      ax.set_xlabel(r'$x/\pi$', fontsize=fontsize) 

     if y_i == 0: 
      ax.set_title(r'$\phi=%s$' % phi[x_i], fontsize=fontsize) 

     if x_i == numPlotsX-1: 
      ax.annotate(r'$A=%d$' % A[x_i], xy=(1.1,0.5), rotation=90, 
         ha='center',va='center',xycoords='axes fraction', fontsize=fontsize) 

f.subplots_adjust(wspace=0,hspace=0) 
plt.suptitle(r'$A\cdot\sin\left(2\pi x + \phi\right)$',fontsize=18) 
plt.show() 
+1

[Dieses Thema] (http://matplotlib.1069221.n5.nabble.com/sharex-with-different-tick-labels-td38913.html) sagt: „* als die Achse geteilt sind, wobei die ticklabels sind auch geteilt * ", also was Sie suchen, könnte unmöglich sein. Siehe eine eng verwandte Q & A [hier] (http://stackoverflow.com/questions/4209467/matplotlib-share-x-axis-but-dont-show-x-axis-tick-labels-for-both-just-one)). –

Antwort

0

Wie @AndrasDeak darauf hingewiesen, ich kann nicht einzelne subplot ändern Zecken, solange ich gesetzt haben sharex = True, Sharey = True. Die Lösung ist, sie zu falschen

f, ax_grid = plt.subplots(numPlotsY,numPlotsX,sharex=False,sharey=False) 

und Ändern der bedingten Anweisungen von meinem ursprünglichen Beitrag auf:

# If left-most column: Remove all overlapping y-ticks 
# Else:     Remove all ticks 
if x_i == 0: 
    ax.set_ylabel(r'$y$', fontsize=fontsize) 
    # If top left subplot:  Remove bottom y-tick 
    # If bottom left subplot: Remove top y-tick 
    # Else:      Remove top and bottom y-ticks 
    if y_i == 0: 
     nbins = len(ax.get_yticklabels()) 
     ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='lower')) 
    elif y_i == numPlotsY-1: 
     nbins = len(ax.get_yticklabels()) 
     ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) 
    else: 
     nbins = len(ax.get_yticklabels()) 
     ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='both')) 
else: 
    ax.yaxis.set_ticks([]) 

# If bottom row: Remove all overlapping x-ticks 
# Else:    Remove all ticks 
if y_i == numPlotsY-1: 
    # If bottom left subplot: Remove right x-tick 
    # If bottom right subplot: Remove top left x-tick 
    # Else:      Remove left and right x-ticks 
    if x_i == 0: 
     nbins = len(ax.get_xticklabels()) 
     ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) 
    elif x_i == numPlotsX-1: 
     nbins = len(ax.get_xticklabels()) 
     ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='lower')) 
    else: 
     nbins = len(ax.get_xticklabels()) 
     ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='both')) 
else: 
    ax.xaxis.set_ticks([]) 

Nicht das eleganteste Ergebnis, aber es zeigt, wie nur zu manipulieren die sich überschneidenden Ticks: enter image description here

Voller Code für jeden interessierten:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import MaxNLocator 

numPlotsY = 3 
numPlotsX = 3 
f, ax_grid = plt.subplots(numPlotsY,numPlotsX,sharex=False,sharey=False) 

A = np.arange(numPlotsY)+1.0 
phi = np.arange(numPlotsX) 
x = np.linspace(0,2.0,100) 
fontsize = 18 

for y_i in range(0,numPlotsY): 
    for x_i in range(0,numPlotsX): 
     y = A[y_i]*np.sin(x*np.pi + phi[x_i]) 
     ax = ax_grid[y_i,x_i] 
     ax.plot(x,y,lw=2.0) 

     # If left-most column: Remove all overlapping y-ticks 
     # Else:     Remove all ticks 
     if x_i == 0: 
      ax.set_ylabel(r'$y$', fontsize=fontsize) 
      # If top left subplot:  Remove bottom y-tick 
      # If bottom left subplot: Remove top y-tick 
      # Else:      Remove top and bottom y-ticks 
      if y_i == 0: 
       nbins = len(ax.get_yticklabels()) 
       ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='lower')) 
      elif y_i == numPlotsY-1: 
       nbins = len(ax.get_yticklabels()) 
       ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) 
      else: 
       nbins = len(ax.get_yticklabels()) 
       ax.yaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='both')) 
     else: 
      ax.yaxis.set_ticks([]) 

     # If bottom row: Remove all overlapping x-ticks 
     # Else:    Remove all ticks 
     if y_i == numPlotsY-1: 
      # If bottom left subplot: Remove right x-tick 
      # If bottom right subplot: Remove top left x-tick 
      # Else:      Remove left and right x-ticks 
      if x_i == 0: 
       nbins = len(ax.get_xticklabels()) 
       ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper')) 
      elif x_i == numPlotsX-1: 
       nbins = len(ax.get_xticklabels()) 
       ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='lower')) 
      else: 
       nbins = len(ax.get_xticklabels()) 
       ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='both')) 
     else: 
      ax.xaxis.set_ticks([]) 

     if y_i == 0: 
      ax.set_title(r'$\phi=%s$' % phi[x_i], fontsize=fontsize) 

     if x_i == numPlotsX-1: 
      ax.annotate(r'$A=%d$' % A[x_i], xy=(1.1,0.5), rotation=90, 
         ha='center',va='center',xycoords='axes fraction', fontsize=fontsize) 

f.subplots_adjust(wspace=0,hspace=0) 
plt.suptitle(r'$A\cdot\sin\left(2\pi x + \phi\right)$',fontsize=18) 
plt.show() 
Verwandte Themen