matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.72k stars 635 forks source link

Chart customization when using Panels.. #398

Open dss010101 opened 3 years ago

dss010101 commented 3 years ago

Hi, i have the following chart i build using the panels approach: image

I need to do some customization of this:

  1. as you can see, i have the volume plotted as a series overlay on the main candle stick panel. I would like to add some opacity to the bar chart as well as have it displayed and scaled down to the lower 1/4th to 1/8th of the panel. Also, is there a concept of a z-order such that the candles are always on top of the bars and moving averages?
  2. I would like to swap the y axis..such that volume is on the right and price on the left
  3. You can see that because the volume is large for this data, it shows in exponential format...i would like to change and reduce the font to try and get it to display, or to have it scaled by 1/1000th perhaps.
  4. there is a lot white space wasted on the chart borders, and im not sure how to control that. i tried adding the tight fit parameter, but it cuts off the right y-axis values. Is there a margin or padding that can help with this and how do you use with panels?
  5. lastly, the separator line in between the panel does not appear for me as it does in the documentation..not sure why

I figure most of the above might be doable, and im just not familiar with the new panels api well enough...but having a hard time finding the docs. Can you point me to where the python docs are for the apis and the various input/parameters?

As an alternative, i tried doing tis by creating the individual figures and axis and passing those to mpl plot. However, when i try to create bar chart (which is needed for my histogram in the picture above), it fails. The line below returns a validation error complaining that 'bars' is not a valid type. when i look at the code, it does seem it is not a supported type for the plot function. is there a work around for this?

                        mpf.plot(s.data, ax = shared_axis, type = 'bars', color = s.color, ylabel='',
                                scale_width_adjustment=dict(lines=0.5),
                                update_width_config=wconfig)

Thanks much

DanielGoldfarb commented 3 years ago

All of the customization you want can be done. However it will be a lot easier to explain what has to change if you would please post your code for the above posted image.

DanielGoldfarb commented 3 years ago
  1. as you can see, i have the volume plotted as a series overlay on the main candle stick panel. I would like to add some opacity to the bar chart

You should should be able to do this with the alpha= kwarg. alpha=0.5 is 50% transparency / 50% opacity. Since you have not shown your code I don't know how you are building the volume bars. Presumably you are using mpf.make_addplot(data,type=bar)? If so, then add alpha= kwarg.


as well as have it displayed and scaled down to the lower 1/4th to 1/8th of the panel.

The simplest way is with kwarg ylim=(ymin,ymax), setting ymax to be about 4x to 8x the maximum volume. (Alternatively you could overlay your own Axes object over the existing Axes, where your Axes object is shorter than the existing one. This is more complicated because (a) you do it externally after calling `mpf.plot(), and (b) you will have to extract the measurements from the existing Axes objects).


Also, is there a concept of a z-order such that the candles are always on top of the bars and moving averages?

It should be relatively easy to implement a z-order kwarg in mplfinance, and we plan to; it just has not been done yet.


  1. I would like to swap the y axis..such that volume is on the right and price on the left

You can do this with y_on_right=False and y_on_right=True


  1. You can see that because the volume is large for this data, it shows in exponential format...i would like to change and reduce the font to try and get it to display, or to have it scaled by 1/1000th perhaps.

  1. there is a lot white space wasted on the chart borders, and im not sure how to control that. i tried adding the tight fit parameter, but it cuts off the right y-axis values. Is there a margin or padding that can help with this and how do you use with panels?

Try the scale_padding kwarg


  1. lastly, the separator line in between the panel does not appear for me as it does in the documentation..not sure why

Ideally, I would have to see your code to try to figure out what is going on. As I'm thinking about it now, this may have something to do with the mplfinance style that you are using. The various styles set axes.edgecolor. As you can see from the link, some styles, like "charles" and "yahoo" use an edge color very similar to the face color, so you won't necessarily see the edge of each panel. Other styles, such as "default" and "classic" use a black edge color against a lighter face so you can easily see the Axes edges between panels.