Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i apply a patch on the chart using X axis values? #179

Closed
Dave-12-cr opened this issue Jun 18, 2020 · 2 comments
Closed

How can i apply a patch on the chart using X axis values? #179

Dave-12-cr opened this issue Jun 18, 2020 · 2 comments
Labels
question Further information is requested

Comments

@Dave-12-cr
Copy link

Dave-12-cr commented Jun 18, 2020

I created a candle chart with fig, axlist = mpf.plot(df, type='candle', returnfig=True)

I'm trying to add on my chart a patch, here is what i tried:

new_patch = Rectangle(xy=(3, 9300), width=4, height=0.3, angle=0, color='orange')
ax1 = axlist[0]
ax1.add_patch(new_patch)
ax1.autoscale_view()

This code works, but the problem is that, instead of specifying the Y value with a number, i would like to specify it with dates, since my X axis has dates in the following format:

Date
2020-06-18 10:30:00   
2020-06-18 10:35:00   
2020-06-18 10:40:00   
2020-06-18 10:45:00

I tried the following: new_patch = Rectangle(xy='2020-06-18 10:30:00',9300), width=4, height=0.3, angle=0, color='orange')

But that will give me an error since X is not int. I just moved to the new library from the older one, i really like all the new features, i'm just having some troubles drawing on the chart like i did with the older one! Any kind of help is appreciated!

@Dave-12-cr Dave-12-cr added the question Further information is requested label Jun 18, 2020
@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Jun 18, 2020

Hi @Dave-12-cr
There are a few things you can try. First, it is important to know that when show_nontrading=False (the default value) then the x-axis is simply the data point index number (from 0 up to N-1 where N is the number of rows in your dataframe).

  1. You can try setting show_nontrading=True. This will cause the x-axis to be matplotlib dates. This may work, although you may have to convert '2020-06-18 10:30:00' into a matplotlib date first.

  2. You can also try passing in the index number closest to the datetime you want. For example, if '2020-06-18 10:30:00' is on the 12th row of your dataframe, you would pass in 11.0 as the x-axis value (counting from zero to the 12th row). As another example, if '2020-06-18 10:30:00' represents a time that is halfway between the 16th row and the 17th row, then you would pass in 15.5 (thus interpolating the location).

  3. If all you want is a rectangle, this can be done using the mplfinance alines kwarg to draw a short line with a large width as described here.

  4. Similarly for a rectangle, or any other shape that can be used as a marker in matplotlib, you can use mpf_makeaddplot(data,type='scatter',marker=...) to place the shape (marker) anywhere on the plot. You can see some examples of doing this here and further discussion about this approach throughout Add a number or a symbol above/below a candle chart with mplfinance? #97

Personally I recommend number 3 or number 4 above as possibly being the simplest to implement; but give whichever one you want a try and let me know how it turns out for you.

@Dave-12-cr
Copy link
Author

@DanielGoldfarb
Wow, so many ways to do this! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants