Skip to content

[BUG] Formatting the Y axis when using MCAP to use K, M, B, T #559

Description

@maxbrand99

Requirements (place an x in each of the [ ])**

  • I realize finplot is not a web lib. (Hint: it's native!)
  • I've read the snippets and not found what I'm looking for.
  • I've searched for any related issues and avoided creating a duplicate issue.
  • I've updated finplot (pip install -U finplot).
  • I've supplied the required data to run my code below.

Code to reproduce

import pandas as pd
import finplot as fplt
import json
import os
import pyqtgraph as pg

# Disable GUI for faster loading
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
# IF LINUX, COMMENT LINE 10
os.environ['QT_QPA_FONTDIR'] = r'C:\Windows\Fonts'

fplt.right_margin_candles = 0
fplt.y_pad = 0.2
fplt.display_timezone = None

class MyAxis(fplt.EpochAxisItem):
    @staticmethod
    def my_timestamp_str(df, row):
        row = max(0, min(len(df) - 1, int(row)))
        timestamp = pd.to_datetime(df.iloc[row, 0], unit='ns')
        return timestamp.strftime('%H:%M')  # Returns HH:MM format
    
    def tickStrings(self, values, scale, spacing):
        return [self.my_timestamp_str(self.vb.datasrc.df, value) for value in values]

def format_number(value) -> str:
    try:
        num = float(value)
    except ValueError:
        return "Invalid number"
    if num >= 1_000_000_000_000:
        return f"{num / 1_000_000_000_000:.3g}T"
    elif num >= 1_000_000_000:
        return f"{num / 1_000_000_000:.3g}B"
    elif num >= 1_000_000:
        return f"{num / 1_000_000:.3g}M"
    elif num >= 1_000:
        return f"{num / 1_000:.3g}K"
    elif num >= 1:
        return f"{value:.4g}"
    else:
        return f"{value:.3f}"


def create_styled_chart(bars: dict, output_file: str, circulating_supply: float) -> None:
    # Prepare DataFrame
    pd.set_option("display.float_format", "{:.18f}".format)
    df = pd.DataFrame({
        'time': pd.to_datetime(bars['t'], unit='s'),
        'open': bars['o'],
        'high': bars['h'],
        'low': bars['l'],
        'close': bars['c'],
    }).set_index('time')
    
    # Create figure with 2 rows (price and volume)
    ax: pg.PlotItem = fplt.create_plot('Chart', rows=1, maximize=False)
    
    if circulating_supply is not None:
        ohlc_cols = ['open', 'high', 'low', 'close']
        df[ohlc_cols] *= float(circulating_supply)
        ax.setLabel('right', 'MCAP')
    
    # Set window size for better resolution
    ax.vb.win.resize(1200, 800)
    
    # set format for the X axis
    ax.setAxisItems({'bottom': MyAxis(vb=ax.vb, orientation='bottom')})
    # Create an InfiniteLine at the given position
    
    # hide the crosshair
    ax.crosshair.hide()
    
    # Set values and frame for the chart
    ax.showAxes([True, True, True, True], [False, False, True, True])
    
    # enable grid and set alpha
    ax.showGrid(x=True, y=True, alpha=0.1)
    
    # hide the top and left grids
    ax.getAxis('top').setGrid(False)
    ax.getAxis('left').setGrid(False)
    
    # Plot candlesticks
    fplt.candlestick_ochl(df[['open', 'close', 'high', 'low']], ax=ax)
    
    # Apply formatter to right axis of volume plot
    ax.getAxis('right').tickStrings = lambda values, scale, spacing: [format_number(v) for v in values]
    
    def _save_and_exit():
        fplt.screenshot(open(output_file, 'wb'))
        fplt.close()
    
    fplt.timer_callback(_save_and_exit, seconds=0.1)
    fplt.show()


def main():
    bars = json.load(open(f"bars.json"))
    create_styled_chart(bars, f"styled_chart.jpg", 1_000_000_000)


if __name__ == '__main__':
    main()

bars.json:

{"o": [0.283999852508, 0.335316021098, 0.295541710454, 0.321526110764, 0.295751925678, 0.397482936085, 0.36025548894, 0.428130186204, 0.441320792324, 0.552209600451, 0.573691129144, 0.580792552631, 0.521610668341, 0.532370994498, 0.46788489482, 0.419148639146, 0.447059378317, 0.453169495368, 0.522803840273, 0.421601984967, 0.417901389125, 0.51068540821, 0.497161087904, 0.443863724608, 0.553505185572, 0.508960883388, 0.761526640842, 0.754995142613, 0.880391127649, 0.890969376161, 0.801789866062, 0.916071353874, 0.80951055698, 0.896280586529, 0.835118288755, 0.732045774934, 0.815474444918, 0.896264109184, 0.932422892114, 1.12606377303, 1.09094673236, 1.1398783687, 1.02822370825, 1.03112195876, 1.07576263149, 1.09246931489, 1.1116746359, 1.24532289733, 1.19454079821, 1.15206579498, 1.0867928709, 1.08377382193, 1.13330242062, 1.08169465468, 1.01938177933, 1.24558067935, 1.37169098886, 1.42240748151, 1.34746115605, 1.37935255064, 1.4351511097, 1.32628096598, 1.29896404031, 1.18965733618, 1.20287185944, 1.35621850064, 1.20917283495, 1.29672335052, 1.40793906224, 1.55546428094, 1.4703519131, 1.43157601229, 1.45398698675, 1.38652366072, 1.36016159113, 1.29900160736, 1.20621595871, 1.0533751381, 1.07114391146, 1.14726256139, 1.14493403546, 1.03104168786, 0.941715549, 1.02963666573, 0.965012135571, 1.00355047532, 1.03566556244, 1.2245672824, 1.38734149458, 1.36031373323, 1.22487566318, 1.28677003061, 1.1886988897, 1.21055341119, 1.17927195424, 1.09724309769, 1.11964232466, 1.03823981734, 0.932901167229, 0.908358931056], "h": [0.34685074526, 0.357619711245, 0.349779955879, 0.321727431306, 0.414912818176, 0.414166126569, 0.43534829941, 0.4847923919, 0.554936379021, 0.64372886281, 0.601604914293, 0.639316769025, 0.596197991676, 0.543293018021, 0.47064430787, 0.486711904944, 0.461795263195, 0.548371166554, 0.56605970575, 0.447004632086, 0.541822377162, 0.540037430866, 0.498659596003, 0.56548109577, 0.61682381747, 0.784675462069, 0.781605422275, 0.983059742492, 0.963023267388, 0.904893253048, 0.961213968066, 0.948020601725, 0.964338037676, 0.93316895232, 0.841930989007, 0.823380729646, 0.899039525604, 0.936056575694, 1.20961603014, 1.19572310992, 1.16225839614, 1.15127445365, 1.0654732668, 1.12086837518, 1.23002117275, 1.16240610412, 1.26118003669, 1.28195052183, 1.22288136653, 1.15206579498, 1.12410942114, 1.14701927411, 1.14925339394, 1.12372687721, 1.24845397307, 1.44798511395, 1.43155494161, 1.43705552992, 1.4713148465, 1.46965405657, 1.46878576225, 1.35755079763, 1.33470449912, 1.21815000684, 1.35621850064, 1.37350352238, 1.32125888518, 1.44379131185, 1.57966083337, 1.64296345569, 1.50724363461, 1.45750577455, 1.47057945492, 1.46288799092, 1.38173488119, 1.3335609912, 1.21211361073, 1.1135909676, 1.17144946, 1.15223569906, 1.20532734852, 1.08354370266, 1.07741398747, 1.08678835669, 1.06390143666, 1.07357528693, 1.23989644346, 1.39212611693, 1.53185308762, 1.37723679914, 1.29682049758, 1.28742274162, 1.25690560548, 1.32012608433, 1.20476092308, 1.15399258445, 1.14157138577, 1.06030273369, 0.947677653178, 0.928658031], "l": [0.283999852508, 0.295524460264, 0.295541710454, 0.265781101456, 0.295315110585, 0.340318952906, 0.344593530017, 0.401455919429, 0.43314424089, 0.519425773993, 0.513122024324, 0.505115910819, 0.521208957143, 0.440888538781, 0.402799969073, 0.407427655699, 0.381975744651, 0.447832133229, 0.412067935659, 0.350227893197, 0.404288476458, 0.451582024067, 0.416022809911, 0.408300938373, 0.507788790573, 0.485570158322, 0.677862074035, 0.733972796222, 0.828759811989, 0.781307951215, 0.797821745992, 0.806258465888, 0.794614593878, 0.812026171491, 0.717259143057, 0.727244679083, 0.805842105199, 0.82540086068, 0.920877015273, 1.05867058053, 1.03426222429, 1.01435984701, 0.9928789557, 0.945852273791, 1.03819865566, 1.07430262274, 1.06575214816, 1.17858565969, 1.14783923588, 1.07207663884, 1.06391974019, 1.05385709949, 1.00480292847, 0.928153462418, 0.995654840939, 1.17343801238, 1.29614511106, 1.27375035673, 1.28440246069, 1.25029427929, 1.31151166178, 1.22204763246, 1.18294419305, 1.15790707724, 1.19333216204, 1.16322513993, 1.19048779591, 1.27627473973, 1.40187364855, 1.44874195485, 1.42980932345, 1.34266588442, 1.37696564217, 1.33867934237, 1.22476451021, 1.20559706662, 1.03314422721, 1.00301196915, 1.04099763069, 1.06731109929, 0.945830098462, 0.923458791612, 0.861326542087, 0.948907157797, 0.956734383002, 0.98573465499, 1.02226996552, 1.20164137857, 1.31606897826, 1.22022535749, 1.06643288081, 1.11923799594, 1.15539292172, 1.17927195424, 1.08585930531, 1.0464874611, 0.995616072297, 0.88152524569, 0.804031444601, 0.858044603758], "c": [0.335316021098, 0.295541710454, 0.321526110764, 0.295751925678, 0.397482936085, 0.36025548894, 0.428130186204, 0.441320792324, 0.552209600451, 0.573691129144, 0.580792552631, 0.521610668341, 0.532370994498, 0.46788489482, 0.419148639146, 0.447059378317, 0.453169495368, 0.522803840273, 0.421601984967, 0.417901389125, 0.51068540821, 0.497161087904, 0.443863724608, 0.553505185572, 0.508960883388, 0.761526640842, 0.754995142613, 0.880391127649, 0.890969376161, 0.801789866062, 0.916071353874, 0.80951055698, 0.896280586529, 0.835118288755, 0.732045774934, 0.815474444918, 0.896264109184, 0.932422892114, 1.12606377303, 1.09094673236, 1.1398783687, 1.02822370825, 1.03112195876, 1.07576263149, 1.09246931489, 1.1116746359, 1.24532289733, 1.19454079821, 1.15206579498, 1.0867928709, 1.08377382193, 1.13330242062, 1.08169465468, 1.01938177933, 1.24558067935, 1.37169098886, 1.42240748151, 1.34746115605, 1.37935255064, 1.4351511097, 1.32628096598, 1.29896404031, 1.18965733618, 1.20287185944, 1.35621850064, 1.20917283495, 1.29672335052, 1.40793906224, 1.55546428094, 1.4703519131, 1.43157601229, 1.45398698675, 1.38652366072, 1.36016159113, 1.29900160736, 1.20621595871, 1.0533751381, 1.07114391146, 1.14726256139, 1.14493403546, 1.03104168786, 0.941715549, 1.02963666573, 0.965012135571, 1.00355047532, 1.03566556244, 1.2245672824, 1.38734149458, 1.36031373323, 1.22487566318, 1.28677003061, 1.1886988897, 1.21055341119, 1.17927195424, 1.09724309769, 1.11964232466, 1.03823981734, 0.932901167229, 0.908358931056, 0.864512066055], "t": [1741996800, 1742083200, 1742169600, 1742256000, 1742342400, 1742428800, 1742515200, 1742601600, 1742688000, 1742774400, 1742860800, 1742947200, 1743033600, 1743120000, 1743206400, 1743292800, 1743379200, 1743465600, 1743552000, 1743638400, 1743724800, 1743811200, 1743897600, 1743984000, 1744070400, 1744156800, 1744243200, 1744329600, 1744416000, 1744502400, 1744588800, 1744675200, 1744761600, 1744848000, 1744934400, 1745020800, 1745107200, 1745193600, 1745280000, 1745366400, 1745452800, 1745539200, 1745625600, 1745712000, 1745798400, 1745884800, 1745971200, 1746057600, 1746144000, 1746230400, 1746316800, 1746403200, 1746489600, 1746576000, 1746662400, 1746748800, 1746835200, 1746921600, 1747008000, 1747094400, 1747180800, 1747267200, 1747353600, 1747440000, 1747526400, 1747612800, 1747699200, 1747785600, 1747872000, 1747958400, 1748044800, 1748131200, 1748217600, 1748304000, 1748390400, 1748476800, 1748563200, 1748649600, 1748736000, 1748822400, 1748908800, 1748995200, 1749081600, 1749168000, 1749254400, 1749340800, 1749427200, 1749513600, 1749600000, 1749686400, 1749772800, 1749859200, 1749945600, 1750032000, 1750118400, 1750204800, 1750291200, 1750377600, 1750464000, 1750550400]}

Describe the bug

It seems to not want to display correct letters on the Y axis. It instead only displays K even tho the number is in the billions (see expected image)

Expected behavior

Display K, M, B, T on the Y axis (for the example i gave, it should be M and B for millions and billions

Screenshots

If applicable, add screenshots to help explain your problem.
with the formatter:
Image

without the formatter:
Image

expected:
Image

Reproducible in:

OS: Windows and Linux (if linux, comment out line 10)
finplot version: 1.9.7
pyqtgraph version: 0.13.7
pyqt5 version: 5.15.11
pyqt6 version: 6.9.1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions