-
Notifications
You must be signed in to change notification settings - Fork 653
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
compatibility with mpld3 #132
Comments
Tega, Just took a quick glance though, and can make an uneducated guess: maybe you need to first call Also, as a workaround you may use mplfinance's ability to save the figure to a png or jpg file, and then perhaps mpld3 has a method to display an image file?? Sorry I cannot be more helpful at this time. If you find the solution, please post it here. Otherwise I will probably circle back and take a closer look at this in a couple of weeks. |
Have any of you actually tried any of the suggestions that I made here? So for example, maybe something like fig, ax = mpf.plot(candles, type='candle', returnfig=True, style='nightclouds')
mpld3.fig_to_html(fig) or similarly, maybe Please list here all of things that you have tried. |
Same image as above while opening test.html |
I spent some time playing with mpld3 and mplfinance and matplotlib, and I have discovered that the problem is with mpld3, in that it does not handle matplotlib's It appears that mpld3 has classified this issue as an enhancement and has been aware of the issue since 2014. If I have some time, I will look into alternatives to mplfinance using I have tested mpld3 with mplfinance in external axes mode and it worked, however it appears that mpld3 also does a lot of its own formatting of labels, and ticks, etc. This is true even when using plain matplotlib (not mplfinance). This means that whatever mplfinance style you may choose, there is always a possibility that mpld3 will modify that style in whatever ways it chooses. Keep in mind also that, when in external axes mode, some mplfinance functionality becomes unavailable, and, if desired, must be implemented externally by the caller. One such example is the mplfinance's |
Hi @DanielGoldfarb, I have tried external axes mode. The chart looks good. But the x-axis turned out to be a numbers instead of date. df = pd.read_json(os.getcwd()+'/screener/misc/curl.json')
Thanks |
@balajiperumaal Balaji, there are two things that I would suggest you try. First some background: When plotting dates or datetimes on the x-axis, matplotlib presumes time is continuous. Therefore all datetimes between the minimum x-axis datetime value and the maximum x-axis datetime value are included along the x-axis. In other words, non-trading dates will be part of the plot, and there will be gaps in the plot for weekends and holidays. Most people prefer not to see these gaps. Therefore the default value for kwarg But as mentioned above, matplotlib presumes that time is continuous (a somewhat reasonable assumption). Therefore the way to implement not plotting non-trading days is to make the x-axis not datetimes, but simply the row number in the dataframe. This ensures that there are no gaps in the plot. However, if we want to format the x-axis with datetimes, then we need to map from dataframe row number to the datetime for that row. You can see mplfinance doing this in the code here. With that background understanding, here are two suggestions to try:
from mplfinance._utils import IntegerIndexDateTimeFormatter
import matplotlib.dates as mdates
dates = mdates.date2num(df.index.tz_localize(None).to_pydatetime())
formatter = IntegerIndexDateTimeFormatter(dates,'%b %d') # '%b %d' can be any strftime() format you want
ax.xaxis.set_major_formatter(formatter) I would be interested to know if this works. (Note: |
I tried using formatter with no luck. When I use Thanks for your elaborated answer |
@balajiperumaal print(data.index)
print(type(data.index))
print(type(data.index[0])) I assume your code is still as shown here. If not, please indicate full code. Also, if possible, please post your data input file somewhere that I can access it. If time permits I will look into this during the week. |
Hi @DanielGoldfarb here is the screenshot of the required variables. I've also tried to remove Thanks |
Hi @balajiperumaal , previously I also encounter this kind of issue but with
On a side note, I have my columns in the right order but there is still a problem. The date doesn’t have the right format and since it is the index, I cannot manipulate it. Therefore, I reset the index and then convert the datetime to a number by using:
|
hi thank you for this awesome library.

I've been trying to display a candlestick chart, which works well on ipython notebook, but with mpld3, all that displays is this:
instead of this:

`df = retrieve_data()
candles = pd.DataFrame( {'Date': pd.to_datetime(df['time']), 'Open': df.Open, 'High': df.High, 'Low': df.Low, 'Close': df.Close, 'Volume': df.Volume})
candles = candles[['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
candles = candles.set_index(pd.DatetimeIndex(candles["Date"]))
fig, ax = mpf.plot(candles, type='candle', returnfig=True, style='nightclouds')
mpld3.show()`
The text was updated successfully, but these errors were encountered: