2013-06-16 16 views
24

Qualcuno può aiutarmi a impostare le zecche su una posizione fissa usando matplotlib? Ho provato con FixedPosition come questo tutorial descrive:Come impostare le tacche su Fixed Position, matplotlib

ax = pl.gca() 
ax.xaxis.set_major_locator(eval(locator)) 

http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#figures-subplots-axes-and-ticks

Ma quando provo a correre, mi dice che il metodo set_major_locator non esiste.

Un semplice esempio sarebbe molto utile.

Grazie.

+2

Tale nesso è una confusione fuori dal contesto. L'uso di 'eval' potrebbe essere molto intelligente per generare le loro cifre di esempio, non è molto chiaro quale sia il valore di' locator'. – tacaswell

risposta

47

Basta usare ax.set_xticks(positions) o ax.set_yticks(positions).

Ad esempio:

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.set_xticks([0.15, 0.68, 0.97]) 
ax.set_yticks([0.2, 0.55, 0.76]) 
plt.show() 

enter image description here

+2

questo metodo cancellerà i tick esistenti che ho sui grafici. Cosa succede se voglio aggiungere questi tick aggiuntivi senza influenzare quelli esistenti? – dnth

+0

@dnth: puoi 'twinx()' o 'twiny()' il grafico per creare un nuovo asse con le zecche sul lato opposto. –

+0

@dnth Puoi ottenere le posizioni con 'locs = ax.xaxis.get_ticklocs()', modificare quell'elenco per aggiungere le zecche che vuoi, quindi seguire i suggerimenti di Joe con, 'ax.set_xticks (locs)'. – ryanjdillon

1
import numpy as np 
import matplotlib.ticker as ticker 
import matplotlib.pyplot as plt 

name_list = ('Omar', 'Serguey', 'Max', 'Zhou', 'Abidin') 
value_list = np.random.randint(0, 99, size = len(name_list)) 
pos_list = np.arange(len(name_list)) 

ax = plt.axes() 
ax.xaxis.set_major_locator(ticker.FixedLocator((pos_list))) 
ax.xaxis.set_major_formatter(ticker.FixedFormatter((name_list))) 
plt.bar(pos_list, value_list, color = '.75', align = 'center') 

plt.show() 
+0

Cura per elaborare? –

+0

Ciò è stato utile. – ConanG

Problemi correlati