簡體   English   中英

Python Matplotlib:在plot_date中更改顏色

[英]Python Matplotlib: Changing color in plot_date

我想繪制一個數據,該數據的x axis具有日期時間值,而另一組值則為y 作為示例,我將使用matplotlib中的示例 ,其中y在這種情況下是股票價格。 這是代碼。

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()

現在,我要做的是根據某種標准為圖表中的每個值上色 為了簡單起見,假設示例中的條件基於年份。 也就是說,屬於同一年的價格將使用相同的顏色。 我該怎么做? 謝謝!

您可以將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()

這使,

在此處輸入圖片說明

我通常這樣做的方法是使用for循環繪制數據的不同部分,並在進行時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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM