2012-06-29 15 views
14

अक्सर मैं गणना का बार चार्ट बनाना चाहता हूं। यदि गणना कम है तो मुझे अक्सर प्रमुख और/या मामूली टिक स्थान मिलते हैं जो पूर्णांक नहीं होते हैं। मेरे द्वारा इसे कैसे रोका जा सकता है? डेटा की गणना होने पर 1.5 पर टिक टिकने का कोई मतलब नहीं है।पायथन matplotlib पूर्णांक टिक स्थानों तक सीमित

यह मेरा पहला प्रयास है:

import pylab 
pylab.figure() 
ax = pylab.subplot(2, 2, 1) 
pylab.bar(range(1,4), range(1,4), align='center') 
major_tick_locs = ax.yaxis.get_majorticklocs() 
if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1: 
    ax.yaxis.set_major_locator(pylab.MultipleLocator(1)) 
minor_tick_locs = ax.yaxis.get_minorticklocs() 
if len(minor_tick_locs) < 2 or minor_tick_locs[1] - minor_tick_locs[0] < 1: 
    ax.yaxis.set_minor_locator(pylab.MultipleLocator(1)) 

जो ठीक काम करता है जब गिनती छोटे हैं लेकिन जब वे बड़े हैं, मैं कई कई छोटे टिक मिलती है:

import pylab 
ax = pylab.subplot(2, 2, 2) 
pylab.bar(range(1,4), range(100,400,100), align='center') 
major_tick_locs = ax.yaxis.get_majorticklocs() 
if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1: 
    ax.yaxis.set_major_locator(pylab.MultipleLocator(1)) 
minor_tick_locs = ax.yaxis.get_minorticklocs() 
if len(minor_tick_locs) < 2 or minor_tick_locs[1] - minor_tick_locs[0] < 1: 
    ax.yaxis.set_minor_locator(pylab.MultipleLocator(1)) 

मैं कैसे प्राप्त कर सकते हैं दूसरे उदाहरण में क्या होता है इससे बचने के दौरान छोटे मामलों के साथ पहले उदाहरण से वांछित व्यवहार?

+0

यह गलत तरीके से डुप्लिकेट के रूप में चिह्नित किया गया है। यह दूसरे प्रश्न से पहले पूछा गया था। दूसरा सवाल डुप्लिकेट के रूप में चिह्नित किया जाना चाहिए। – John

उत्तर

24

तुम इतनी तरह, MaxNLocator विधि का उपयोग कर सकते हैं::

from pylab import MaxNLocator 

    ya = axes.get_yaxis() 
    ya.set_major_locator(MaxNLocator(integer=True)) 
+1

मुझे विश्वास है कि 'pylab.MaxNLocator()' बेहतर संकेत है। – FooBar

+2

या यदि आप पिलैब आयात नहीं कर रहे हैं, तो 'matplotlib.ticker.MaxNLocator() '। ([इस उत्तर] से (http://stackoverflow.com/a/27496811/2452770)) –

0

मुझे लगता है कि यह पता चला है कि मैं सिर्फ मामूली टिकों को अनदेखा कर सकता हूं।

def ticks_restrict_to_integer(axis): 
    """Restrict the ticks on the given axis to be at least integer, 
    that is no half ticks at 1.5 for example. 
    """ 
    from matplotlib.ticker import MultipleLocator 
    major_tick_locs = axis.get_majorticklocs() 
    if len(major_tick_locs) < 2 or major_tick_locs[1] - major_tick_locs[0] < 1: 
     axis.set_major_locator(MultipleLocator(1)) 

def _test_restrict_to_integer(): 
    pylab.figure() 
    ax = pylab.subplot(1, 2, 1) 
    pylab.bar(range(1,4), range(1,4), align='center') 
    ticks_restrict_to_integer(ax.xaxis) 
    ticks_restrict_to_integer(ax.yaxis) 

    ax = pylab.subplot(1, 2, 2) 
    pylab.bar(range(1,4), range(100,400,100), align='center') 
    ticks_restrict_to_integer(ax.xaxis) 
    ticks_restrict_to_integer(ax.yaxis) 

_test_restrict_to_integer() 
pylab.show() 
2
pylab.bar(range(1,4), range(1,4), align='center') 

और

मैं इस एक स्थान दें और देखें कि क्या यह सब उपयोग के मामलों में खड़ा करने के लिए जा रहा हूँ
xticks(range(1,40),range(1,40)) 

ने मेरे कोड में काम किया है। बस align वैकल्पिक पैरामीटर का उपयोग करें और xticks जादू करता है।

+0

क्या यह विधि बड़ी श्रेणियों के लिए बहुत अधिक टिक नहीं देती है? – John

संबंधित मुद्दे