2011-11-04 20 views
10

पर तारीखों के साथ सबप्लॉट्स मुझे एक्स-अक्ष पर तिथियों के साथ एकाधिक सबप्लॉट्स का उपयोग करने में समस्या हो रही है।एक्स-अक्ष

मैं here से matplotlib उदाहरण का उपयोग कर रहा हूं। मैंने इसे एक और सबप्लॉट शामिल करने के लिए संशोधित किया है (प्लॉट किया जा रहा डेटा समान है)। यह वही मैं आउटपुट के रूप में हो रही है है:

enter image description here

टिक केवल दूसरे subplot पर दिखाई देते हैं। क्यूं कर? मैं उन्हें सबप्लॉट्स पर कैसे दिखा सकता हूं?

यहां मेरा संशोधित स्रोत है। मैंने स्रोत के माध्यम से if ब्लॉक में एक नया सबप्लॉट शामिल करने के लिए कोड जोड़ा है।

#!/usr/bin/env python 
""" 
Show how to make date plots in matplotlib using date tick locators and 
formatters. See major_minor_demo1.py for more information on 
controlling major and minor ticks 

All matplotlib date plotting is done by converting date instances into 
days since the 0001-01-01 UTC. The conversion, tick locating and 
formatting is done behind the scenes so this is most transparent to 
you. The dates module provides several converter functions date2num 
and num2date 

""" 
import datetime 
import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import matplotlib.mlab as mlab 
import matplotlib.cbook as cbook 

years = mdates.YearLocator() # every year 
months = mdates.MonthLocator() # every month 
yearsFmt = mdates.DateFormatter('%Y') 

# load a numpy record array from yahoo csv data with fields date, 
# open, close, volume, adj_close from the mpl-data/example directory. 
# The record array stores python datetime.date as an object array in 
# the date column 
#datafile = cbook.get_sample_data('goog.npy') 
datafile = 'goog.npy' 
r = np.load(datafile).view(np.recarray) 

fig = plt.figure() 
ax = fig.add_subplot(211) 
ax.plot(r.date, r.adj_close) 


# format the ticks 
ax.xaxis.set_major_locator(years) 
ax.xaxis.set_major_formatter(yearsFmt) 
ax.xaxis.set_minor_locator(months) 

datemin = datetime.date(r.date.min().year, 1, 1) 
datemax = datetime.date(r.date.max().year+1, 1, 1) 
ax.set_xlim(datemin, datemax) 

# format the coords message box 
def price(x): return '$%1.2f'%x 
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') 
ax.format_ydata = price 
ax.grid(True) 

second = True 
if second: 
    years = mdates.YearLocator() # every year 
    months = mdates.MonthLocator() # every month 
    yearsFmt = mdates.DateFormatter('%Y') 

    ax = fig.add_subplot(212) 
    ax.plot(r.date, r.adj_close) 

    # format the ticks 
    ax.xaxis.set_major_locator(years) 
    ax.xaxis.set_major_formatter(yearsFmt) 
    ax.xaxis.set_minor_locator(months) 

    datemin = datetime.date(r.date.min().year, 1, 1) 
    datemax = datetime.date(r.date.max().year+1, 1, 1) 
    ax.set_xlim(datemin, datemax) 

    # format the coords message box 
    ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') 
    ax.format_ydata = price 
    ax.grid(True) 

# rotates and right aligns the x labels, and moves the bottom of the 
# axes up to make room for them 
fig.autofmt_xdate() 

plt.show() 

उत्तर

11

मुझे अपराधी मिला है। यह autofmt_xdate समारोह है:

दिनांक ticklabels अक्सर ओवरलैप, तो यह उन्हें बारी बारी से करने और सही उन्हें संरेखित उपयोगी है। साथ ही, एक सामान्य उपयोग केस साझा xaxes के साथ कई उप-स्थान हैं जहां एक्स-अक्ष दिनांक डेटा है। टिकलबेल अक्सर लंबे होते हैं, और यह उन्हें नीचे सबप्लॉट पर घुमाने में मदद करता है और उन्हें अन्य सबप्लॉट्स पर बंद कर देता है, साथ ही xlabels को बंद कर देता है।

यह एक "सुविधा" है। आप प्रत्येक subplot के बाद इस कोड को डालने से एक ही प्रभाव को प्राप्त कर सकते हैं:

plt.xticks(rotation=30) 
+0

अतिरिक्त ध्यान दें: यदि आप प्रत्येक भूखंड के लिए अलग आंकड़े का उपयोग करें, यह किया जाना इससे पहले कि आप 'plt.figure()' फिर से फोन की जरूरत है। – thegrinner