Closed 9dhealth closed 2 years ago
Most likely there is some discrepency between where you have installed mplfinance, and how you are importing it, or possibly your are importing something else on top of it.
I have to see all of your code to have a better idea. Also, it will help to see how you installed mplfinance, and where your install is located. At the command prompt or unix prompt, try running:
python -c 'import mplfinance as mpf;print(mpf.__file__)'
python -c 'import sys;print(sys.path)'
and post the output here with the code.
I used pyqtgraph before but I find mplfinance is cool. My python version is 3.8.8. I use pycharm and anaconda both.
1) I tried two methods: pip install --upgrade mplfinance or by pycharm (→setting→python interpreter). Both show Successfully installed mplfinance-0.12.7a17. (d:\users\administrator\appdata\local\programs\python\python38\lib\site-packages)
2)
import pandas as pd
import tushare as ts
import mplfinance as mpf
from pylab import mpl
pd.set_option('expand_frame_repr', False)
pd.set_option('display.max_columns', None)
pd.set_option('colheader_justify', 'centre')
pro = ts.pro_api("") # it should a own api.
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
df = pro.daily(ts_code='000001.SZ', start_date='20171001', end_date='20211020')
data = df.loc[:, ['trade_date', 'open', 'close', 'high', 'low', 'vol']]
data = data.rename(columns={'trade_date': 'Date', 'open': 'Open', 'close': 'Close', 'high': 'High', 'low': 'Low', 'vol': 'Volume'})
data.set_index('Date', inplace=True)
data.index = pd.DatetimeIndex(data.index)
data = data.sort_index(ascending=True)
print(data)
mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, show_nontrading=False)`
I can print(data)
Open Close High Low Volume
Date
2017-10-09 11.57 11.30 11.64 11.26 1325227.31
2017-10-10 11.33 11.47 11.50 11.33 747925.30
2017-10-11 11.48 11.53 11.58 11.34 658077.58
2017-10-12 11.54 11.55 11.58 11.47 578065.31
2017-10-13 11.56 11.36 11.56 11.25 737375.96
... ... ... ... ... ...
2020-07-27 13.67 13.24 13.68 13.10 1880653.35
2020-07-28 13.34 13.34 13.43 13.18 1217005.99
2020-07-29 13.35 13.54 13.63 13.21 1519580.25
2020-07-30 13.50 13.37 13.51 13.37 964067.63
2020-07-31 13.28 13.34 13.53 13.25 1165821.91
[687 rows x 5 columns]
Traceback (most recent call last):
File "E:/PycharmProjects/Stock/mpl.py", line 27, in <module>
mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, show_nontrading=False)
AttributeError: module 'mplfinance' has no attribute 'plot'
I think your problem may have something to do with importing mpl
from pylab
. This may be overriding the symbol plot
in mplfinance.
At any rate, importing matplotlib and/or pyplot from matplotlib should never be necessary when using mplfinance. One of the main goals of mplfinance is to provide a simplified layer over matplotlib. (Although there are times when it may be necessary to import individual objects for the object oriented interface of matplotlib, for the sake of accomplishing things that are not yet supporte in matplotlib, but only through the object oriented interface of matplotlib).
Additionally mplfinance has its own way of handling rcParams
and it is inadvisable to circumvent it. When you want to customize any rcParams
in mplfinance, this should always be done via mplfinance styles. Please read the mplfinance styles tutorial (if you have not done so already) and use the kwarg rc=
to method mpf.make_mpf_style()
.
Bottom line, I suggest making the following changes and see if it works:
as follows:
4d3
< from pylab import mpl
11,12c10,14
< mpl.rcParams['font.sans-serif'] = ['SimHei']
< mpl.rcParams['axes.unicode_minus'] = False
---
>
> s = mpf.make_mpf_style(base_mpf_style='default',
> rc={'font.sans-serif':'SimHei',
> 'axes.unicode_minus':False})
>
20c22
< mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, show_nontrading=False)`
---
> mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, style=s)
Notice that as I added style=s
in the call to mpf.plot()
, I also removed show_nontrading=False
. This is only because the default value of show_nontrading
is False
and so it is not necessary to specify it.
Please let me know if the above suggestion helps. Thanks. --Daniel
One last note: If the above suggestion does not work, please show the rest of your code. The reason I ask this is because the error message:
File "E:/PycharmProjects/Stock/mpl.py", line 27, in <module>
mpf.plot(data, type='candle', mav=(5, 10, 20), volume=True, show_nontrading=False)
AttributeError: module 'mplfinance' has no attribute 'plot'
indicates the error occurs on line 27, but in the code you posted the call to mpf.plot()
is on line 20, so I suspect that some code is not shown, and it is possible that something else may be causing the problem.
Also, I have heard over the past year various reports of issues when using pycharm. Some people have no problems with pycharm, and other people do have problems, and we have not been about to figure out why. Please also try without pycharm.
pip uninstall mplfinance
pip install --no-cache-dir mplfinance
Install and run without running pycharm at all, and see if that helps.
Thank you.
Ask anything you want about mplfinance usage, project philosophy and/or priorities, or anything else related to mplfinance. I tried many version but it don't work.