作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Matplotlib 上创建了一个烛台图,现在我想在上面画更多东西。
这是我的代码::
...
fig = plt.figure(facecolor='#131722',dpi=135)
#ax = fig.add_subplot(1,1,1)
ax1 = plt.subplot2grid((6,4), (1,0), rowspan=4, colspan=4, facecolor='#131722')
candlestick2_ohlc(ax1, opens, highs, lows, closes, width=FINALWIDTH, alpha=1,colorup='#53B987', colordown='#EB4D5C')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(8))
xdate = [datetime.fromtimestamp(i) for i in dates]
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(20)
def mydate(x,pos=None):
try:
if CandleFrame == '1D' or CandleFrame == '4H':
return xdate[int(x)].strftime('%m/%d %H:%M')
else:
t = xdate[int(x)].strftime('%m/%d %H:%M')
print(t)
return xdate[int(x)].strftime('%m/%d %H:%M')
except IndexError:
return ''
#return pl.num2date(x).strftime('%Y-%m-%d')
ax1.xaxis.set_major_formatter(mticker.FuncFormatter(mydate))
ax1.grid(False, color='#242938', alpha=0.5, ls='dotted')
ax1.spines['bottom'].set_color("#131722")
ax1.spines['top'].set_color("#131722")
ax1.spines['left'].set_color("#131722")
ax1.spines['right'].set_color("#131722")
ax1.tick_params(axis='both', colors='w')
ax1.set_axisbelow(True)
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator())
try:
plt.hlines(y=9125, xmin='05/13 05:30', xmax='05/13 10:30', color='g')
except Exception as e:
print(e)
plt.cla()
plt.close()
...
fig = plt.figure(facecolor='#131722',dpi=135)
#ax = fig.add_subplot(1,1,1)
ax1 = plt.subplot2grid((6,4), (1,0), rowspan=4, colspan=4, facecolor='#131722')
candlestick2_ohlc(ax1, opens, highs, lows, closes, width=FINALWIDTH, alpha=1,colorup='#53B987', colordown='#EB4D5C')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(8))
xdate = [datetime.fromtimestamp(i) for i in dates]
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(20)
def mydate(x,pos=None):
try:
if CandleFrame == '1D' or CandleFrame == '4H':
return xdate[int(x)].strftime('%m/%d %H:%M')
else:
t = xdate[int(x)].strftime('%m/%d %H:%M')
print(t)
return xdate[int(x)].strftime('%m/%d %H:%M')
except IndexError:
return ''
#return pl.num2date(x).strftime('%Y-%m-%d')
ax1.xaxis.set_major_formatter(mticker.FuncFormatter(mydate))
ax1.grid(False, color='#242938', alpha=0.5, ls='dotted')
ax1.spines['bottom'].set_color("#131722")
ax1.spines['top'].set_color("#131722")
ax1.spines['left'].set_color("#131722")
ax1.spines['right'].set_color("#131722")
ax1.tick_params(axis='both', colors='w')
ax1.set_axisbelow(True)
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator())
plt.cla()
plt.close()
[datetime.datetime(2020, 5, 14, 22, 40), datetime.datetime(2020, 5, 14, 22, 45), datetime.datetime(2020, 5, 14, 22, 50), datetime.datetime(2020, 5, 14, 22, 55), datetime.datetime(2020, 5, 14, 23, 0), datetime.datetime(2020, 5, 14, 23, 5), datetime.datetime(2020, 5, 14, 23, 10), datetime.datetime(2020, 5, 14, 23, 15), datetime.datetime(2020, 5, 14, 23, 20), datetime.datetime(2020, 5, 14, 23, 25), datetime.datetime(2020, 5, 14, 23, 30), datetime.datetime(2020, 5, 14, 23, 35), datetime.datetime(2020, 5, 14, 23, 40), datetime.datetime(2020, 5, 14, 23, 45), datetime.datetime(2020, 5, 14, 23, 50), datetime.datetime(2020, 5, 14, 23, 55), datetime.datetime(2020, 5, 15, 0, 0), datetime.datetime(2020, 5, 15, 0, 5), datetime.datetime(2020, 5, 15, 0, 10), datetime.datetime(2020, 5, 15, 0, 15), datetime.datetime(2020, 5, 15, 0, 20), datetime.datetime(2020, 5, 15, 0, 25), datetime.datetime(2020, 5, 15, 0, 30), datetime.datetime(2020, 5, 15, 0, 35), datetime.datetime(2020, 5, 15, 0, 40), datetime.datetime(2020, 5, 15, 0, 45), datetime.datetime(2020, 5, 15, 0, 50), datetime.datetime(2020, 5, 15, 0, 55), datetime.datetime(2020, 5, 15, 1, 0), datetime.datetime(2020, 5, 15, 1, 5), datetime.datetime(2020, 5, 15, 1, 10), datetime.datetime(2020, 5, 15, 1, 15), datetime.datetime(2020, 5, 15, 1, 20), datetime.datetime(2020, 5, 15, 1, 25), datetime.datetime(2020, 5, 15, 1, 30), datetime.datetime(2020, 5, 15, 1, 35), datetime.datetime(2020, 5, 15, 1, 40), datetime.datetime(2020, 5, 15, 1, 45), datetime.datetime(2020, 5, 15, 1, 50), datetime.datetime(2020, 5, 15, 1, 55), datetime.datetime(2020, 5, 15, 2, 0), datetime.datetime(2020, 5, 15, 2, 5), datetime.datetime(2020, 5, 15, 2, 10), datetime.datetime(2020, 5, 15, 2, 15), datetime.datetime(2020, 5, 15, 2, 20), datetime.datetime(2020, 5, 15, 2, 25), datetime.datetime(2020, 5, 15, 2, 30), datetime.datetime(2020, 5, 15, 2, 35), datetime.datetime(2020, 5, 15, 2, 40), datetime.datetime(2020, 5, 15, 2, 45), datetime.datetime(2020, 5, 15, 2, 50), datetime.datetime(2020, 5, 15, 2, 55), datetime.datetime(2020, 5, 15, 3, 0), datetime.datetime(2020, 5, 15, 3, 5), datetime.datetime(2020, 5, 15, 3, 10), datetime.datetime(2020, 5, 15, 3, 15), datetime.datetime(2020, 5, 15, 3, 20), datetime.datetime(2020, 5, 15, 3, 25), datetime.datetime(2020, 5, 15, 3, 30), datetime.datetime(2020, 5, 15, 3, 35), datetime.datetime(2020, 5, 15, 3, 40), datetime.datetime(2020, 5, 15, 3, 45), datetime.datetime(2020, 5, 15, 3, 50), datetime.datetime(2020, 5, 15, 3, 55), datetime.datetime(2020, 5, 15, 4, 0), datetime.datetime(2020, 5, 15, 4, 5), datetime.datetime(2020, 5, 15, 4, 10), datetime.datetime(2020, 5, 15, 4, 15), datetime.datetime(2020, 5, 15, 4, 20), datetime.datetime(2020, 5, 15, 4, 25), datetime.datetime(2020, 5, 15, 4, 30), datetime.datetime(2020, 5, 15, 4, 35), datetime.datetime(2020, 5, 15, 4, 40), datetime.datetime(2020, 5, 15, 4, 45), datetime.datetime(2020, 5, 15, 4, 50), datetime.datetime(2020, 5, 15, 4, 55), datetime.datetime(2020, 5, 15, 5, 0), datetime.datetime(2020, 5, 15, 5, 5), datetime.datetime(2020, 5, 15, 5, 10), datetime.datetime(2020, 5, 15, 5, 15), datetime.datetime(2020, 5, 15, 5, 20), datetime.datetime(2020, 5, 15, 5, 25), datetime.datetime(2020, 5, 15, 5, 30), datetime.datetime(2020, 5, 15, 5, 35), datetime.datetime(2020, 5, 15, 5, 40), datetime.datetime(2020, 5, 15, 5, 45), datetime.datetime(2020, 5, 15, 5, 50), datetime.datetime(2020, 5, 15, 5, 55), datetime.datetime(2020, 5, 15, 6, 0), datetime.datetime(2020, 5, 15, 6, 5), datetime.datetime(2020, 5, 15, 6, 10), datetime.datetime(2020, 5, 15, 6, 15), datetime.datetime(2020, 5, 15, 6, 20), datetime.datetime(2020, 5, 15, 6, 25), datetime.datetime(2020, 5, 15, 6, 30), datetime.datetime(2020, 5, 15, 6, 35), datetime.datetime(2020, 5, 15, 6, 40), datetime.datetime(2020, 5, 15, 6, 45), datetime.datetime(2020, 5, 15, 6, 50), datetime.datetime(2020, 5, 15, 6, 55), datetime.datetime(2020, 5, 15, 7, 0), datetime.datetime(2020, 5, 15, 7, 5), datetime.datetime(2020, 5, 15, 7, 10), datetime.datetime(2020, 5, 15, 7, 15), datetime.datetime(2020, 5, 15, 7, 20), datetime.datetime(2020, 5, 15, 7, 25), datetime.datetime(2020, 5, 15, 7, 30), datetime.datetime(2020, 5, 15, 7, 35), datetime.datetime(2020, 5, 15, 7, 40), datetime.datetime(2020, 5, 15, 7, 45), datetime.datetime(2020, 5, 15, 7, 50), datetime.datetime(2020, 5, 15, 7, 55), datetime.datetime(2020, 5, 15, 8, 0), datetime.datetime(2020, 5, 15, 8, 5), datetime.datetime(2020, 5, 15, 8, 10), datetime.datetime(2020, 5, 15, 8, 15), datetime.datetime(2020, 5, 15, 8, 20), datetime.datetime(2020, 5, 15, 8, 25), datetime.datetime(2020, 5, 15, 8, 30), datetime.datetime(2020, 5, 15, 8, 35), datetime.datetime(2020, 5, 15, 8, 40), datetime.datetime(2020, 5, 15, 8, 45), datetime.datetime(2020, 5, 15, 8, 50), datetime.datetime(2020, 5, 15, 8, 55), datetime.datetime(2020, 5, 15, 9, 0), datetime.datetime(2020, 5, 15, 9, 5), datetime.datetime(2020, 5, 15, 9, 10), datetime.datetime(2020, 5, 15, 9, 15), datetime.datetime(2020, 5, 15, 9, 20), datetime.datetime(2020, 5, 15, 9, 25), datetime.datetime(2020, 5, 15, 9, 30), datetime.datetime(2020, 5, 15, 9, 35), datetime.datetime(2020, 5, 15, 9, 40), datetime.datetime(2020, 5, 15, 9, 45), datetime.datetime(2020, 5, 15, 9, 50), datetime.datetime(2020, 5, 15, 9, 55), datetime.datetime(2020, 5, 15, 10, 0), datetime.datetime(2020, 5, 15, 10, 5), datetime.datetime(2020, 5, 15, 10, 10), datetime.datetime(2020, 5, 15, 10, 15)]
myData = [[9320, datetime.datetime(2020, 5, 15, 00, 20)'05/15 00:20'], [9440, datetime.datetime(2020, 5, 15, 8, 43)] ... ]
x=9320
处的图表上应该有一条小线、一个圆或一个小矩形(无论如何正确可视化)。在对应于时间的蜡烛下方
'05/15 00:20'
, 所以它应该和
x
处的蜡烛一样大观点。
plt.hlines(y=9320, xmin=?, xmax=?, color='g')
最佳答案
有几种方法。我发现最简单的方法是复制您关心的数据点的补丁(或矩形/条),然后将其设置为 y
对新事物的值(value)。
由于您没有提供数据,因此我以 ebay 的一些旧股票价格为例。我无法获得 matplotlib.finance
的确切版本您正在使用,因为它已被弃用。稍后我可能会在虚拟环境中安装旧版本,但我确实让它在最新的 mplfinance
中工作库虽然具有与您正在使用的功能几乎相同的功能(我将在最后介绍如何更新到此模块):
From the Matplotlib API :
matplotlib.finance.candlestick_ochl(...)
returns(lines, patches)
wherelines
is a list of lines added andpatches
is a list of the rectangle patches added
candlestick_ochl
的返回中获取这些补丁。 ,复制你关心的那个,然后改变它的一些属性,比如颜色和位置。所有属性以及如何更改它们
are in the documentation :
import copy
...
lines, patches = candlestick_ohlc(ax1, ohlc[:5], width=0.4, colorup='#77d879', colordown='#db3f3f')
new_patch = copy.copy(patches[-3]) # must use copy, or you'll modify the original Rectangle. This grabs the 3rd one (right to left)
new_patch.set_y(148)
new_patch.set_color('white')
new_patch.set_height(0.3)
print(new_patch)
Rectangle(xy=(736534, 148), width=0.4, height=0.3, angle=0)
# Add the patch to the Axes
ax1.add_patch(new_patch)
ax1.autoscale_view()
from matplotlib.patches import Rectangle
...
new_patch = Rectangle(xy=(736530, 155), width=0.4, height=0.3, angle=0, color='white')
ax1.add_patch(new_patch)
ax1.autoscale_view()
def add_highlight(patch_to_highlight, y_position, color, height):
new_patch = copy.copy(patch_to_highlight)
new_patch.set_y(y_position)
new_patch.set_color(color)
new_patch.set_height(height)
ax1.add_patch(new_patch)
ax1.autoscale_view()
mplfinanace
:
import copy
import urllib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.patches import Rectangle
import matplotlib.ticker as mticker
from mplfinance.original_flavor import candlestick_ohlc
def bytespdate2num(fmt, encoding='utf-8'):
strconverter = mdates.strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter
def add_highlight(ax, patch_to_highlight, y_position, color='white', height=0.3):
new_patch = copy.copy(patch_to_highlight)
new_patch.set_y(y_position)
new_patch.set_color(color)
new_patch.set_height(height)
ax.add_patch(new_patch)
ax.autoscale_view()
def graph_data(stock):
# This is some old ebay stock price data
stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'
source_code = urllib.request.urlopen(stock_price_url).read().decode()
stock_data = []
split_source = source_code.split('\n')
for line in split_source[1:]:
split_line = line.split(',')
if len(split_line) == 7:
if 'values' not in line and 'labels' not in line:
stock_data.append(line)
# parse and organize the data
date, closep, highp, lowp, openp, _, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y-%m-%d')})
x = 0
y = len(date)
ohlc = []
while x < y:
append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
ohlc.append(append_me)
x+=1
# do the plotting
plt.style.use('dark_background')
plt.figure()
ax1 = plt.subplot2grid((1, 1), (0, 0))
lines, patches = candlestick_ohlc(ax1, ohlc[:5], width=0.4, colorup='#53B987', colordown='#EB4D5C')
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
## Method with copying existing patch
# new_patch = copy.copy(patches[-3])
# new_patch.set_y(148)
# new_patch.set_color('orange')
# new_patch.set_height(0.3)
# ax1.add_patch(new_patch)
# ax1.autoscale_view()
## Method with making new Rectangle
# new_patch = Rectangle(xy=(736530, 155), width=0.4, height=0.3, angle=0, color='orange')
# ax1.add_patch(new_patch)
# ax1.autoscale_view()
## Method using a function (cleanest)
add_highlight(ax1, patches[-3], 136)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(stock)
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.tight_layout()
plt.savefig('example.png')
plt.show()
graph_data('EBAY')
mplfinance
mplfinance
,使用点子:
pip install mplfinance
from mplfinance.original_flavor import candlestick_ohlc
lines, patches = candlestick_ohlc((ax1, opens, highs, lows, closes, width=FINALWIDTH, alpha=1, colorup='#53B987', colordown='#EB4D5C')
关于python - 如何在 Matplotlib 上的两个数据点之间绘制一条水平线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61776352/
我是一名优秀的程序员,十分优秀!