2016-11-01 19 views
0

x axisのdatetime値とyの値を持つデータをプロットするとします。一例として、私はこの場合はyが株価であるmatplotlibからexampleを使用します。ここにそのコードがあります。今Python Matplotlib:plot_dateの色を変更する

import matplotlib.pyplot as plt 
from matplotlib.finance import quotes_historical_yahoo_ochl 
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter 
import datetime 
date1 = datetime.date(1995, 1, 1) 
date2 = datetime.date(2004, 4, 12) 

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

quotes = quotes_historical_yahoo_ochl('INTC', date1, date2) 
if len(quotes) == 0: 
    raise SystemExit 

dates = [q[0] for q in quotes] 
opens = [q[1] for q in quotes] 

fig, ax = plt.subplots() 
ax.plot_date(dates, opens, '-') 

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

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

fig.autofmt_xdate() 
plt.show() 

、私がやりたいことはいくつかの基準に基づいて、グラフ内の各値色です。簡潔にするために、例の場合の基準が年に基づいているとしましょう。つまり、同じ年に属する価格は同じ色になります。どうすればいい?ありがとう!

+0

散布図または線図として? 2つの異なる年の間にラインをどのように色づけしますか? – GWW

+0

を線図として表示します。 2つの異なる年の間に、前年の色を使用しましょう。 – jtitusj

答えて

1

あなたは(この場合は年に)したい範囲にわたって、マスクでnumpyの配列を使用することができます。あなたの例から作り付けYearLocator機能を利用するためには、あなたが最初のグラフをプロットし、ダニを設定し、その後、あなたの例から、年間の範囲で取り外して交換する必要があり、

import matplotlib.pyplot as plt 
from matplotlib.finance import quotes_historical_yahoo_ochl 
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter 
import datetime 
import numpy 

date1 = datetime.date(1995, 1, 1) 
date2 = datetime.date(2004, 4, 12) 

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

quotes = quotes_historical_yahoo_ochl('INTC', date1, date2) 
if len(quotes) == 0: 
    raise SystemExit 

dates = np.array([q[0] for q in quotes]) 
opens = np.array([q[1] for q in quotes]) 

fig, ax = plt.subplots() 
l = ax.plot_date(dates, opens, '-') 

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

l[0].remove() 
py = years()[0] 
for year in years()[1:]: 
    mask = (py < dates) & (dates < year) 
    ax.plot_date(dates[mask], opens[mask], '-') 
    py = year 

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

fig.autofmt_xdate() 
plt.show() 

与える、

enter image description here

2

一般的には、forループを使用してデータの異なるセクションをプロットし、各セクションに色分けします。あなたの例では、このセクション:

fig, ax = plt.subplots() 
ax.plot_date(dates, opens, '-') 

は次のようになります。

# import the colormaps 
from maplotlib import cm 

fig, ax = plt.subplots() 

for y in years: 
    y_indices = [i for i in range(len(dates)) if dates[i].year==y] 

    # subset the data, there are better ways to do this 
    sub_dates = [dates[i] for i in y_indices] 
    sub_opens = [opens[i] for i in y_indices] 

    # plot each section of data, using a colormap to change the color for 
    # each iteration. 
    ax.plot_date(sub_dates, sub_opens, '-', linecolor=cm.spring((y-2000)/10.0) 
関連する問題