Mplfinance: logarithmic scale

Created on 19 May 2020  路  8Comments  路  Source: matplotlib/mplfinance

is it possible to set y axis (price) scale to logarithmic scale?

question

Most helpful comment

Here is how I do it

from decimal import Decimal
import mplfinance as mpf
import matplotlib.ticker as ticker

def format_price(x, _=None):
    x = Decimal(x)
    return x.quantize(Decimal(1)) if x == x.to_integral() else x.normalize()

fig, axlist = mpf.plot(ohlc, ..., returnfig=True)
ax1 = axlist[0]
ax1_minor_yticks = ax1.get_yticks(True)  # save the original ticks because the log ticks are sparse
ax1_major_yticks = ax1.get_yticks(False)
ax1.set_yscale('log')
ax1.set_yticks(ax1_major_yticks, True)
ax1.set_yticks(ax1_minor_yticks, False)
ax1.yaxis.set_major_formatter(ticker.FuncFormatter(format_price))
ax1.yaxis.set_minor_formatter(ticker.FuncFormatter(format_price))

thanks char101. i don't know why but i get this error:
KeyError: 'Unrecognized kwarg="returnfig"'

All 8 comments

presently no. this will be worked on in the near future (see #21), perhaps as early as a few a weeks from now. in the meantime, you may try the returnfig=True kwarg, to get back the figure and axes. Set the y-scale, and then plt.show(). I have not tried it, but that may be a work-around. Otherwise, as I said, will work on this in a few weeks, as there are several other enhancements presently in flight. all the best. --Daniel

Here is how I do it

from decimal import Decimal
import mplfinance as mpf
import matplotlib.ticker as ticker

def format_price(x, _=None):
    x = Decimal(x)
    return x.quantize(Decimal(1)) if x == x.to_integral() else x.normalize()

fig, axlist = mpf.plot(ohlc, ..., returnfig=True)
ax1 = axlist[0]
ax1_minor_yticks = ax1.get_yticks(True)  # save the original ticks because the log ticks are sparse
ax1_major_yticks = ax1.get_yticks(False)
ax1.set_yscale('log')
ax1.set_yticks(ax1_major_yticks, True)
ax1.set_yticks(ax1_minor_yticks, False)
ax1.yaxis.set_major_formatter(ticker.FuncFormatter(format_price))
ax1.yaxis.set_minor_formatter(ticker.FuncFormatter(format_price))

presently no. this will be worked on in the near future (see #21), perhaps as early as a few a weeks from now. in the meantime, you may try the returnfig=True kwarg, to get back the figure and axes. Set the y-scale, and then plt.show(). I have not tried it, but that may be a work-around. Otherwise, as I said, will work on this in a few weeks, as there are several other enhancements presently in flight. all the best. --Daniel

Thank you Daniel.
I will wait for it...

Here is how I do it

from decimal import Decimal
import mplfinance as mpf
import matplotlib.ticker as ticker

def format_price(x, _=None):
    x = Decimal(x)
    return x.quantize(Decimal(1)) if x == x.to_integral() else x.normalize()

fig, axlist = mpf.plot(ohlc, ..., returnfig=True)
ax1 = axlist[0]
ax1_minor_yticks = ax1.get_yticks(True)  # save the original ticks because the log ticks are sparse
ax1_major_yticks = ax1.get_yticks(False)
ax1.set_yscale('log')
ax1.set_yticks(ax1_major_yticks, True)
ax1.set_yticks(ax1_minor_yticks, False)
ax1.yaxis.set_major_formatter(ticker.FuncFormatter(format_price))
ax1.yaxis.set_minor_formatter(ticker.FuncFormatter(format_price))

thanks char101. i don't know why but i get this error:
KeyError: 'Unrecognized kwarg="returnfig"'

thanks char101. i don't know why but i get this error:
KeyError: 'Unrecognized kwarg="returnfig"

Check your version number: mpf.__version__

If necessary, pip install --upgrade mplfinance

The most up to date release is version 0.12.4a0

Hi Daniel
I use this style for my plot:
mc = mpf.make_marketcolors(up='#0db104', down='r',
edge='#3B3B3B',
volume='#37536d',
)
s = mpf.make_mpf_style(marketcolors=mc, gridaxis='both', gridcolor='#DCDCDC')
and my plot is:
image

but after using char101 suggestion method for log y axis my horizontal grids at that axes disappears!
image

do you know why this happens and how to resolve it?

You'll need to modify the major ticks not the minor ticks. Your 2nd chart only shows minor ticks and no major ticks while the grid is rendered at major ticks positions.

You'll need to modify the major ticks not the minor ticks. Your 2nd chart only shows minor ticks and no major ticks while the grid is rendered at major ticks positions.

Thanks.
i used this line to fix it:
ax1.yaxis.grid(True, which='minor')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franklee00 picture franklee00  路  4Comments

cleitonmoya picture cleitonmoya  路  5Comments

simonholzapfel picture simonholzapfel  路  5Comments

Full4me picture Full4me  路  3Comments

jainraje picture jainraje  路  4Comments