如何在每个月的第一天显示主要价格变动,并在每天中显示次要价格变动? [英] How do I show major ticks as the first day of each months and minor ticks as each day?

查看:27
本文介绍了如何在每个月的第一天显示主要价格变动,并在每天中显示次要价格变动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循 matplotlib文档创建股票的价格量图.我有一个关于如何将主要刻度设置为每个月的第一天和每天的次要刻​​度的问题.我试图遵循 http://matplotlib.org/examples/pylab_examples/date_demo2.html,但就是无法让它工作.以下是我目前可以获得的最佳结果.有帮助吗?!

I tried to follow the matplotlib documentation to create price-volume plot for stocks. I have a question about how to set the major ticks to the first day of each month and minor ticks for each day. I tried to follow http://matplotlib.org/examples/pylab_examples/date_demo2.html, but just could not get it to work. The following is the best I can get for now. Any help?!

#!/usr/bin/env python

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator, MonthLocator, DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo, candlestick2, volume_overlay
from matplotlib import gridspec
from matplotlib.dates import num2date, IndexDateFormatter
from matplotlib.ticker import  IndexLocator, FuncFormatter

from operator import itemgetter

# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2010, 2, 1)
date2 = (2011, 2, 1)

symbol = 'TSLA'

quotes = quotes_historical_yahoo(symbol, date1, date2)

if len(quotes) == 0:
    raise SystemExit

ds, opens, closes, highs, lows, volumes = zip(*quotes)

def get_locator():
    """
    the axes cannot share the same locator, so this is a helper
    function to generate locators that have identical functionality
    """
    return IndexLocator(10, 1)

formatter =  IndexDateFormatter(ds, '%b %d %y')

def millions(x, pos):
    'The two args are the value and tick position'
    return '%1.1fM' % (x*1e-6)

def thousands(x, pos):
    'The two args are the value and tick position'
    return '%1.1fK' % (x*1e-3)

millionformatter = FuncFormatter(millions)
thousandformatter = FuncFormatter(thousands)

#fig = plt.figure(figsize=(8, 6)) 

fig = plt.figure()
fig.subplots_adjust(bottom=0.15)
fig.subplots_adjust(hspace=0)
fig.suptitle(symbol, fontsize=24, fontweight='bold')

gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) 

ax0 = plt.subplot(gs[0])

#candlestick(ax0, quotes, width=0.6)
candles = candlestick2(ax0, opens, closes, highs, lows, width=1, colorup='g')

ax0.xaxis.set_major_locator( get_locator() )
ax0.xaxis.set_major_formatter(formatter)
ax0.set_ylabel('Price', fontsize=16)

#ax0.xaxis_date()
#ax0.autoscale_view()

ax1 = plt.subplot(gs[1], sharex=ax0)

#vc = volume_overlay3(ax1, quotes, colorup='k', colordown='r', width=4, alpha=1.0)
#volume_overlay(ax1, opens, closes, volumes, colorup='g', alpha=0.5, width=1)
#ax1.set_xticks(ds)

vc = volume_overlay(ax1, opens, closes, volumes, colorup='g', alpha=0.5, width=1)
ax1.add_collection(vc)

#ax1.format_xdata = DateFormatter('%Y-%m-%d')

#maxvolume = max(quotes,key=itemgetter(5))[5]

#ax1.set_ylim([0, maxvolume])

ax1.xaxis.set_major_locator(get_locator())
ax1.xaxis.set_major_formatter(formatter)
ax1.yaxis.set_major_formatter(millionformatter)
ax1.yaxis.tick_right()
ax1.set_ylabel('Volume', fontsize=16)

#ax1.xaxis_date()
#ax1.autoscale_view()

plt.setp(ax0.get_xticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), rotation=90, horizontalalignment='left')

plt.show()

我得到的图片如下:

推荐答案

只为后代:

import matplotlib.dates as dt
import matplotlib.ticker as ticker
ax.xaxis.set_major_locator(dt.MonthLocator())
ax.xaxis.set_major_formatter(dt.DateFormatter('%d %b'))
ax.xaxis.set_minor_locator(dt.DayLocator())
ax.xaxis.set_minor_formatter(ticker.NullFormatter())

这篇关于如何在每个月的第一天显示主要价格变动,并在每天中显示次要价格变动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆