Skip to content

Change plot color #340

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

Closed
geirak opened this issue Feb 25, 2021 · 7 comments
Closed

Change plot color #340

geirak opened this issue Feb 25, 2021 · 7 comments
Labels
question Further information is requested

Comments

@geirak
Copy link

geirak commented Feb 25, 2021

Hi
I have an indicator where I would like to change plot color based on certain criteria indicating a bullish or bearish scenario. Is it possible to specify colors per bar with the mpf.make_addplot color parameter, or can only one color be passed in?
Regards
Geir

@geirak geirak added the question Further information is requested label Feb 25, 2021
@DanielGoldfarb
Copy link
Collaborator

The color kwarg in mpf.make_addplot() is only for plot types line, scatter and bar. Ohlc bars and candles are colored by "marketcolors", which are part of the mpf "style" that you pass in.

That said, I belive what you are looking for will be handled by the following enhancement: #205 (comment)

Let me know if that is what you are looking for.

@DanielGoldfarb
Copy link
Collaborator

@geirak ... I just realized that you already commented on the above mentioned enhancement. However you bring up an interesting point. Until that enhancement is implemented, it may be possible to use mpf.make_addplot() as a work-around. It's a little bit klunky but it may work: Use mpf.make_addplot(type='scatter',color=?) with sparse data (set all data to nan except for where you want the scatter mark to appear) and choose a rectangle or square as a scatter mark and place it right on top of the candle.

@geirak
Copy link
Author

geirak commented Feb 25, 2021

@DanielGoldfarb
Right :-)

The workaround you mention sounds like something I am looking (I saw it in one of your examples files) into for another thing I need.

Here is an example of what I am looking for:

Is it is possible to control the color of a line plot on a bar by bar basis. See attached screen shot (Ninja Trader), where you can see that the slow line changes color when the fast yellow line crosses the slow line. I can deal with all the logic, I just need to pass in the colors on a bar by bar basis

Changing oscillator colors

(sorry about all the edits...)

@DanielGoldfarb
Copy link
Collaborator

@geirak
Interesting. You may be able to do that with two mpf.make_addplot() type=line plots, using two different colors, and where you place nan values in each line where you want to see the other line's color.

@geirak
Copy link
Author

geirak commented Feb 25, 2021

@DanielGoldfarb
right - that would work for me. I'll give it a shot.

Thank you Daniel for your excellent help!!!

@geirak
Copy link
Author

geirak commented Feb 26, 2021

@DanielGoldfarb Daniel - I just wanted to give feedback to show that your proposal works.

Assuming that there are already columns for the plot value ("D") and a column for the plot color ("dcolor") I created two new columns "dbull" and "dbear", which were filled with values only for the corresponding color.

I noticed that there would always be a gap in the plot when the color changed, so I had to ensure that there was an overlap at the color change.

Below a couple of excerpts from how I solved it. I am very new to Python and pandas, so I am sure there are better ways to accomplish this
2021-02-26_14-27-39

Here is how I am plotting:
2021-02-26_14-19-32

And here is the first plot
2021-02-26_14-19-56

Axis etc. still need some more work...

@geirak geirak closed this as completed Feb 26, 2021
@DanielGoldfarb
Copy link
Collaborator

@geirak
Awesome! Thank you so much for sharing. I love to see the creative things people are doing with mplfinance (both the code and the output). I had done a little playing around and also came to the conclusion that a small overlap is needed to avoid a gap at the point of color change. Looks great!
Thanks again. All the best. --Daniel

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