- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
经典的股票价格图表由一条连接每个时间段的最高价和最低价的垂直线组成,左边的标记表示开盘(第一个)价格,右边的标记表示收盘(最后一个)价格。
Matplotlib 可以绘制日文版,称为烛台,但我一直无法找到西文版的解决方案,简称为“条形图”。 Matplotlib 能画出这样的图表吗?
最佳答案
从 matplotlib 财务包(documentation、code)中调整烛台函数:
def westerncandlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
ochl=True, linewidth=0.5):
"""
Plot the time, open, high, low, close as a vertical line ranging
from low to high. Use a rectangular bar to represent the
open-close span. If close >= open, use colorup to color the bar,
otherwise use colordown
Parameters
----------
ax : `Axes`
an Axes instance to plot to
quotes : sequence of quote sequences
data to plot. time must be in float date format - see date2num
(time, open, high, low, close, ...) vs
(time, open, close, high, low, ...)
set by `ochl`
width : float
fraction of a day for the open and close lines
colorup : color
the color of the lines close >= open
colordown : color
the color of the lines where close < open
ochl: bool
argument to select between ochl and ohlc ordering of quotes
linewidth: float
linewidth of lines
Returns
-------
ret : tuple
returns (lines, openlines, closelines) where lines is a list of lines
added
"""
OFFSET = width / 2.0
lines = []
openlines = []
closelines = []
for q in quotes:
if ochl:
t, open, close, high, low = q[:5]
else:
t, open, high, low, close = q[:5]
if close >= open:
color = colorup
else:
color = colordown
vline = Line2D( xdata=(t, t), ydata=(low, high),
color=color, linewidth=linewidth, antialiased=True)
lines.append(vline)
openline = Line2D(xdata=(t - OFFSET, t), ydata=(open,open),
color=color, linewidth=linewidth, antialiased=True)
openlines.append(openline)
closeline = Line2D(xdata=(t , t+OFFSET), ydata=(close,close),
color=color, linewidth=linewidth, antialiased=True)
closelines.append(closeline)
ax.add_line(vline)
ax.add_line(openline)
ax.add_line(closeline)
ax.autoscale_view()
return lines, openlines, closelines
称它为例如像这样:
westerncandlestick(ax, quotes, width=0.6, linewidth=1.44, ochl=False)
当然,您可以使用 colorup
和 colordown
参数调整颜色。
重现上述情节的完整代码:
import matplotlib.pyplot as plt
from matplotlib.finance import quotes_historical_yahoo_ohlc
from matplotlib.lines import Line2D
def westerncandlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
ochl=True, linewidth=0.5):
"""
Plot the time, open, high, low, close as a vertical line ranging
from low to high. Use a rectangular bar to represent the
open-close span. If close >= open, use colorup to color the bar,
otherwise use colordown
Parameters
----------
ax : `Axes`
an Axes instance to plot to
quotes : sequence of quote sequences
data to plot. time must be in float date format - see date2num
(time, open, high, low, close, ...) vs
(time, open, close, high, low, ...)
set by `ochl`
width : float
fraction of a day for the open and close lines
colorup : color
the color of the lines close >= open
colordown : color
the color of the lines where close < open
ochl: bool
argument to select between ochl and ohlc ordering of quotes
linewidth: float
linewidth of lines
Returns
-------
ret : tuple
returns (lines, openlines, closelines) where lines is a list of lines
added
"""
OFFSET = width / 2.0
lines = []
openlines = []
closelines = []
for q in quotes:
if ochl:
t, open, close, high, low = q[:5]
else:
t, open, high, low, close = q[:5]
if close >= open:
color = colorup
else:
color = colordown
vline = Line2D( xdata=(t, t), ydata=(low, high),
color=color, linewidth=linewidth, antialiased=True)
lines.append(vline)
openline = Line2D(xdata=(t - OFFSET, t), ydata=(open,open),
color=color, linewidth=linewidth, antialiased=True)
openlines.append(openline)
closeline = Line2D(xdata=(t , t+OFFSET), ydata=(close,close),
color=color, linewidth=linewidth, antialiased=True)
closelines.append(closeline)
ax.add_line(vline)
ax.add_line(openline)
ax.add_line(closeline)
ax.autoscale_view()
return lines, openlines, closelines
from matplotlib.dates import DateFormatter, WeekdayLocator,\
DayLocator, MONDAY
# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2004, 2, 1)
date2 = (2004, 4, 12)
mondays = WeekdayLocator(MONDAY) # major ticks on the mondays
alldays = DayLocator() # minor ticks on the days
weekFormatter = DateFormatter('%b %d') # e.g., Jan 12
dayFormatter = DateFormatter('%d') # e.g., 12
quotes = quotes_historical_yahoo_ohlc('INTC', date1, date2)
if len(quotes) == 0:
raise SystemExit
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
westerncandlestick(ax, quotes, width=0.6, linewidth=1.44, ochl=False)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
关于python - 如何用matplotlib绘制经典的股票图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810875/
我正在处理现有网站的移动版本,我无法解决菜单中链接的问题。 该问题仅发生在标准的 android 浏览器上。在 Chrome、firefox、safari 甚至 IE 上,该网站都运行良好。该网站上的
几周来我一直在努力解决这个问题,但没有找到真正的解决方案。我发现了一种解决方法,但我觉得它很烦人。 图像在我的 Galaxy S3 的默认浏览器中加载模糊,但在 chrome 和 firefox 中它
安装了多个浏览器。我怎样才能打开http://www.google.com以编程方式使用内置(库存)浏览器? 最佳答案 使用内置浏览器,通常可以通过按菜单按钮使地址栏出现(当然是在按图标打开浏览器之后
我在面试中被问到这样的问题: 给定股票价格: MS | 500 Apl | 1000 Nefx| 500 MS | 500 每次新库存到来时,我们都必须添加到现有库存中,否则如果是新
我需要将每个键的值相乘,然后将所有值相加以打印一个数字。我知道这可能非常简单,但我被卡住了 在我看来,我会用类似的方式来解决这个问题: for v in prices: total = sum(v *
直到昨天这样的查询 http://autoc.finance.yahoo.com/autoc?query=a&callback=YAHOO.Finance.SymbolSuggest.ssCallba
我正在尝试找到一个在phonegap应用程序中绘制折线/股票图表的解决方案。我尝试过很多库:amcharts JS、highcharts,但没有一个能工作。 有人可以帮我完成这个任务吗?欢迎任何解决方
如果您在 Google 上查看股票(例如 search for 'Apple stocks' ),您会得到一个相当漂亮且交互式的图表,如下所示: 请注意垂直十字线和漂亮的工具提示。 事实证明,尝试在
首先,我必须说,我是人工智能方面的初学者。我遵循了大多数有关股市预测的教程,它们几乎都是相同的。这些教程使用一个数据集并分为两组。第一个是训练集,第二个是测试集。他们正在使用股票的收盘价来训练和制作模
最近在使用highchart stock(highstock.js)的时候遇到了一个很奇怪的问题。我加载了一些包含星期六数据点的数据点。当应用程序运行时,起初它看起来像这样: 没有图表出现,只有导航器
我已经在 Azure 中的存储帐户上部署了新的文件共享,自从我这样做以来,我不再能够执行 terraform 计划,而是收到以下错误: azurerm_storage_account_customer
我是一名优秀的程序员,十分优秀!