YONG BAGJNS YONG BAGJNS - 5 months ago 22
Python Question

How to draw a diagonal line in subplot?

I try to draw a diagonal line but it doesn't work.

I can draw a horizontal and vertical line by use axhline and axvline

but I dont know about diagonal I have use plot but it doesn't work

Here is some of my code and result :

while (loop < candleLength) :
date = stockData['date'][loop]
date = dateToFloat(date)
append = date, stockData['open'][loop], stockData['high'][loop], stockData['low'][loop], stockData['close'][loop]
ohlc.append(append)
loop += 1

fig = plt.figure()
ax = plt.subplot2grid((1,1), (0,0))

candlestick_ohlc(ax, ohlc, width=0.66, colorup='#4dff4d', colordown='#ff471a') # plt.plot([736062.0, 736118.0], [70, 70], 'k-', lable = 'example line')


for label in ax.xaxis.get_ticklabels():
label.set_rotation(45)

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.xaxis.set_major_locator(mondays)
ax.grid(True)


ax.text(buyPointTypeFloat, buyPrice, 'BUY', fontdict=font_dict)
ax.text(sellPointTypeFloat, sellPrice, 'SELL', fontdict=font_dict)
# ax.plot(x=buyPrice, y=sellPrice)
ax.axhline(y=sellPrice,color='k',ls='dashed')
ax.axhline(y=buyPrice,color='k',ls='dashed')
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(stockName)
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()


result pic

From pic you will see that i have 2 horizontal line at buy/sell price

but i want to use diogonal line instead.

Thank you.

Answer

well, use a conventional line:

ax.plot((xStart, xEnd), (yStart, yEnd), ...)