2011-11-04 14 views
10

Mam problem z używaniem wielu subplots z datami na osi X.Subplots z datami na osi X

Używam przykładu matplotlib z here. Zmodyfikowałem go tak, aby zawierał inny wątek (nanoszone dane są takie same). To co mi się jako wyjście:

enter image description here

kleszcze pojawiają się tylko na drugim poletko. Czemu? Jak mogę je wyświetlić na obu wątkach?

Oto moje zmodyfikowane źródło. Dodałem kod, aby dołączyć nowy wątek do bloku if w połowie źródła.

#!/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() 

Odpowiedz

11

Znalazłem winnego. Jest to funkcja autofmt_xdate:

Dni ticklebels często nakładają się, dlatego warto je obracać i wyrównywać w prawo. Ponadto powszechnym przypadkiem użycia jest liczba podpól ze wspólnymi xaxami, w których oś X jest datowana. Znaczniki są często długie i pomagają obracać je na dolnym wątku i wyłączać je na innych wątkach, a także wyłączać tablice.

To "funkcja". Można osiągnąć ten sam efekt poprzez wstawienie tego kodu po każdym poletko:

plt.xticks(rotation=30) 
+0

dodatkowe Uwaga: jeśli używasz oddzielne dane dla każdego poletka, to należy zrobić przed wywołaniem 'plt.figure()' ponownie. – thegrinner

Powiązane problemy