0

I have a matplotlib graph that I have created using data from arrays. I want to annotate this graph at certain points. The x axis is populated with dates (14/06/12, 15/06/12) etc.. The y axis is price (6500, 6624) etc... I would like to annotate at point: for example (x,y) (14/06/12, 6500). This is my code so far:

Date = ["14/06/12", "15/06/12"]
Open = [6500, 6544]
High = [5434, 5234]
Low = [5342, 5325]
Close = [4523, 2342]
ohlc = []
i = 0
while i < 2:
    Prices = Date[i], Open[i], High[i], low[i], Close[i]
    ohlc.append(Prices)
    i += 1

candlestick_ohlc(ax, ohlc, width=0.8, colorup='g', colordown='r')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.annotate('Here!', xy=(Date[1], Price[1]))
plt.show()

This is the current graph and I want the annotation on it where i put it: https://i.sstatic.net/5qM4D.jpg

11
  • What's your question, exactly? Commented Oct 16, 2016 at 22:21
  • What is currently happening now in your code that is not giving you the annotation you expect? Commented Oct 16, 2016 at 22:22
  • Also, can you give a sample of what exactly Prices is and what ohlc is exactly? I'm not a matplotlib person, but if someone wanted to assist here and wanted to test out your code, I would assume it would be helpful to give a small sample of the data. Commented Oct 16, 2016 at 22:23
  • Hey guys, updated the question, sorry! Commented Oct 16, 2016 at 22:24
  • Is it important to make use of subplots to perform this annotation? I'm looking at the documentation on annotation examples here: matplotlib.org/users/annotations_intro.html#annotating-text Commented Oct 16, 2016 at 22:25

1 Answer 1

0

Here's a quick example using the matplotlib.pyplot text command to add text to a plot at a specified location:

import numpy as np
import matplotlib.pyplot as plt

plt.figure()
x = np.arange(-5, 5, 0.1)
plt.plot(x, np.cos(x))
plt.text(x=-4, y=0.5, s="Cosine", fontsize=20)
plt.show()

There are lots of additional formatting options available for text (alignment, font, etc.). Full documentation is available here:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.