Skip to content

Bug Report: Unnecessary graphs are displayed when add_subplot is done in mpf.figure() #241

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
r-beginners opened this issue Aug 16, 2020 · 5 comments
Labels
question Further information is requested

Comments

@r-beginners
Copy link

Describe the bug

I wanted to create a list of styles that can be applied to the graph, so I tried the new subplot feature and the completely unrelated graphs are displayed first. After that, the desired graph is displayed normally.

To Reproduce
When you run the following code, the

import datetime
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import mplfinance as mpf
# import matplotlib.pyplot as plt

with open('./alpha_vantage_api_key.txt') as f:
    api_key = f.read()

now_ = datetime.datetime.today()

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(now_.year, now_.month, now_.day - 1)

symbol = 'AAPL'
df = web.DataReader(symbol, 'av-daily', start, end, api_key=api_key)

df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
df.index = pd.to_datetime(df.index)

g_style = mpf.available_styles()

fig = mpf.figure()

for i, style in zip(np.arange(1, 13), g_style):
    ax = fig.add_subplot(12, 1, i)
    print(style)
    ax = mpf.plot(df[1000:], figratio=(16, 9), type='candle', volume=True, mav=(5, 25), style=style)

mpf.show()

Some of the data used are presented below.

df[1000:].head()
	Open	High	Low	Close	Volume
2019-12-23	280.53	284.25	280.37	284.00	24643000
2019-12-24	284.69	284.89	282.92	284.27	12119700
2019-12-26	284.82	289.98	284.70	289.91	23280300
2019-12-27	291.12	293.97	288.12	289.80	36566500
2019-12-30	289.46	292.69	285.22	291.52	36028600

Screenshots
スクリーンショット 2020-08-16 13 16 09

Desktop (please complete the following information):

  • OS: Mac OS 10.13.6
  • Browser Chrom, JupyterLab 2.2.4 Mplfinance 0.12.7a0 matplotlib 3.3.1 pandas 1.1.0 python 3.6.6

This is a bug report, but my code may be wrong, and if so, please point it out to me.

@r-beginners r-beginners added the bug Something isn't working label Aug 16, 2020
@DanielGoldfarb
Copy link
Collaborator

DanielGoldfarb commented Aug 16, 2020

If you want to use "External Axes" mode, then you have to pass your Axes in to mpf.plot()

This line:

 ax = mpf.plot(df[1000:], figratio=(16, 9), type='candle', volume=True, mav=(5, 25), style=style)

should be:

mpf.plot(df[1000:], figratio=(16, 9), type='candle', volume=True, mav=(5, 25), style=style, ax=ax)

Even doing that, keep in mind the following so that you can understand the results you will get: You are creating a Figure with one style, and each of your Axes with other styles, which creates effectively a mixed style which in some cases may have incompatibilities between the styles.

More specifically, since you are not passing any style when creating the Figure with fig = mpf.figure(), the Figure background will be created with style='default' which may be partially incompatible with one or more of the other styles.


You can see an example of getting around one specific incompatibility in the External Axes notebook in Cells [9] though [11]:

The specific incompatibility, in this external axes example, is that style 'nightclouds' normally has white text (see styles, cell [19]), while the Figure in this example uses 'default' style, which has a white background. Therefore it would be impossible to see any of the labels and tickmarks in the 'nightclouds' style. To get around this example incompatibility, you can see in cell [9] of that notebook a custom style is created based on 'nightclouds' but with a different base_mpl_style. This custom style effectively creates a 'nightclouds' style with black text instead of white text, so that it can be seen on the white background.

HTH. Let me know if you have any further questions.

All the best. --Daniel

@r-beginners
Copy link
Author

@daniel Thanks for the confirmation.
I did not check the documentation. The notebook you presented helped me to understand it better.

Corrected code:.

fig = mpf.figure(figsize=(12,36))

for i, style in zip(np.arange(1, 13), g_style):
    ax = fig.add_subplot(6, 2, i, style=style)
    mpf.plot(df[1000:], type='candle', mav=(5, 25), axtitle=style, ax=ax, xrotation=15)

mpf.show()

What my code can't achieve.

  1. volume=True is not enabled
  2. style='nightclouds', 'mike' doesn't display ticks (because you can't set base_mpl_style='fast')

スクリーンショット 2020-08-16 22 21 37

How to deal with it:.

  1. Can I use ax1 and mpf.plot(volume=ax1) with matplotlib?
  2. What are some ways to improve ?

I support further upgrades. Thanks.

@DanielGoldfarb
Copy link
Collaborator

@r-beginners

I strongly recommend that you go carefully through all of the tutorials, and pay particular attention to those under

As I already mentioned above, if you mix incompatible styles (one style for the Figure and one style for the Axes) the you will get undesireable results. In your example above with 'nightclouds' and 'mike', you have two choices: you can either customize 'nightclouds', and 'mike' to have non-white text, or customized the style used for the Figure itself, so that the Figure background is perhaps a color that will contrast with both black text and white text; then you will be able to view all of the pre-packaged Axes styles on a single Figure (even though the Figure is not the same as it would be for each style alone).

Regarding passing in an Axes for volume. There is an example in cells [6] and [7] of the external axes notebook

I would also strongly discourage you from using external axes mode. As stated in the description of external axes mode,

"Passing external Axes into mplfinance results in more complex code but it also provides all the power and flexibility of matplotlib for those who know how to ... use it."

The intention of External Axes mode is to give those who already have a strong understanding and skill level with Matplotlib itself, the ability to use those skills fo to go beyond what mplfinance can do. But mplfinance can already do a lot, and I suggest you first learn everything that it can do without external axes mode, which is already 90% to 95% of typical plots you would expect to see in the financial markets.

Thanks. All the best. --Daniel

@DanielGoldfarb DanielGoldfarb added question Further information is requested and removed bug Something isn't working labels Aug 16, 2020
@DanielGoldfarb
Copy link
Collaborator

For what it's worth, here is an example of using a Figure color that contrasts nicely with both black text and with white text:

fig_style = mpf.make_mpf_style(base_mpf_style='default',figcolor='#3C8284')

fig = mpf.figure(figsize=(12,36),style=fig_style)

for i, style in zip(range(1, 13), g_style):
    ax = fig.add_subplot(6, 2, i, style=style)
    mpf.plot(df, type='candle', mav=(5, 25), axtitle=style, ax=ax, xrotation=15)
mpf.show()

The only problem with doing it this way is that, in practice, none of these styles actually have #3C8284 as their Figure background color. But it does help see all styles from the Axes perspective. The result:

image

@r-beginners
Copy link
Author

@DanielGoldfarb
Thank you for taking your valuable time to give me some thoughtful advice. And thank you for your response to 'nightclouds' and 'mike'. The specific images finally made sense to me. I now understand the design of mplfinance for the code as well.

DanielGoldfarb added a commit that referenced this issue Dec 22, 2020
New handling of volume exponent; and assorted scratch_pad additions:

This PR is primarily to implement a new way to handle the volume exponent; the new way avoids calling `.draw()` which in turn eliminates the bug mentioned in issue #296, and also mentioned/described in a previous email from S.G. ([commit 2c88663](2c88663))

This PR also includes a lot of scratch_pad work for investigations of various issues (#282, #236, #241), and for an mplfinance presentation: including comparison of old and new api's, intraday animation with volumes, generation of animation gifs, etc.
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