Skip to content
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

logarithmic scale #138

Closed
mlmrtz opened this issue May 19, 2020 · 8 comments
Closed

logarithmic scale #138

mlmrtz opened this issue May 19, 2020 · 8 comments
Labels
question Further information is requested

Comments

@mlmrtz
Copy link

mlmrtz commented May 19, 2020

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

@mlmrtz mlmrtz added the question Further information is requested label May 19, 2020
@DanielGoldfarb
Copy link
Collaborator

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

@char101
Copy link

char101 commented May 19, 2020

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))

@mlmrtz
Copy link
Author

mlmrtz commented May 19, 2020

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...

@mlmrtz
Copy link
Author

mlmrtz commented May 19, 2020

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"'

@DanielGoldfarb
Copy link
Collaborator

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

@mlmrtz
Copy link
Author

mlmrtz commented Jun 12, 2020

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?

@char101
Copy link

char101 commented Jun 12, 2020

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.

@mlmrtz
Copy link
Author

mlmrtz commented Jun 12, 2020

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')

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

3 participants