Search code examples
pythonpandasmatplotlibplotbinance

Unable to plot multiple lines in a single graph


I am having a weird issue, I am trying to plot multiple lines in a single graph but it is only one. I am sharing the screenshot as you can see the close values are different in both. It is not rendering binance graph as it seems to be overridden.

enter image description here

Graph

enter image description here

Update

The code is given below

# All Imports
import ccxt
import pandas as pd
import matplotlib.pyplot as plt
# Connect binance
binance = ccxt.binance()
ftx = ccxt.ftx()
binance_btc_usdt_ohlcv = binance.fetch_ohlcv('BTC/USDT','1d',limit=100)
ftx_btc_usdt_ohlcv = ftx.fetch_ohlcv('BTC/USDT','1d',limit=100)
df_binance = pd.DataFrame(binance_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
df_ftx = pd.DataFrame(ftx_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
fig, ax = plt.subplots()
ax.plot(df_binance['ts'], df_binance['v'],label='Binance')
ax.plot(df_ftx['ts'], df_ftx['v'],label='FTX')

plt.legend()
# ax.tick_params(axis='x', colors='red')
plt.show()

Solution

    • Tested in python 3.8.12, pandas 1.3.3, matplotlib 3.4.3

    Existing Code

    • Works without any issues, however, 'Binance' is small compared to 'FTX', which can be resolved with ax.set_yscale('log')
    df_binance = pd.DataFrame(binance_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
    df_ftx = pd.DataFrame(ftx_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
    fig, ax = plt.subplots()
    ax.plot(df_binance['ts'], df_binance['v'], label='Binance')
    ax.plot(df_ftx['ts'], df_ftx['v'], label='FTX')
    
    ax.legend()
    ax.set_yscale('log')  # resolve issues of scale with the y-axis values
    plt.show()
    

    enter image description here

    • Without ax.set_yscale('log'), 'Binance' still shows up on the plot

    enter image description here


    • The text code example in the OP used 'v', but the issue was occuring with 'c' (in the screenshot).
      • The issue is df_ftx.c and df_binance.c are almost exactly the same, which we can see by using alpha=0.5.
    # plot dataframe
    ax = df_binance.plot(x='ts', y='c', label='Binance', figsize=(8, 6), logy=True)
    p2 = df_ftx.plot(x='ts', y='c', label='FTX', ax=ax, alpha=0.5)
    
    ax.legend(bbox_to_anchor=(1, 1.02), loc='upper left')
    plt.show()
    

    enter image description here

    • Given the scale of the y-axis, the difference is too small to differentiate the two lines.
    >>> df_binance.c.sub(df_ftx.c)
    
    0      1.00
    1     -2.13
    2      0.07
    3     -2.44
    4     -0.35
    5      1.35
    6     11.51
    7     -6.17
    8    -11.91
    9     -2.86
    10   -13.98
    11    -7.40
    12    -3.13
    13     1.56
    14   -15.52
    15    -8.63
    16     0.83
    17    10.44
    18     0.82
    19    -0.95
    20   -12.82
    21    -2.54
    22   -15.13
    23   -14.46
    24    -4.63
    25   -12.60
    26   -10.01
    27   -17.00
    28    -4.00
    29   -16.00
    30    -9.49
    31    -5.18
    32    -3.71
    33    23.95
    34    -4.71
    35    -2.38
    36   -11.53
    37    -7.13
    38   -10.78
    39     1.85
    40     0.01
    41    -9.68
    42     7.87
    43     9.90
    44    -4.65
    45     2.83
    46     5.91
    47    -3.11
    48   -14.48
    49   -11.36
    50    -0.86
    51     2.64
    52   -22.12
    53    -8.10
    54    -6.27
    55    -3.69
    56    -0.86
    57     1.91
    58     5.69
    59     1.24
    60    -1.27
    61   -12.48
    62    -1.59
    63    -8.18
    64     5.98
    65    -6.26
    66    -4.25
    67    -2.38
    68    11.38
    69    -9.39
    70    -4.74
    71    -0.43
    72    -9.36
    73    -3.10
    74    -0.65
    75     1.54
    76    -2.72
    77    -1.90
    78    -0.39
    79    -9.10
    80    -4.99
    81    -6.06
    82     6.99
    83     0.00
    84    -8.78
    85     2.43
    86    -2.28
    87   -10.00
    88    -9.65
    89    -5.07
    90    -1.00
    91    -0.06
    92   -28.58
    93    -8.43
    94    -8.67
    95   -17.16
    96    -3.41
    97   -12.59
    98    -1.85
    99     5.99
    Name: c, dtype: float64
    

    Updated Code

    • Convert 'ts' to a datetime dtype with pd.to_datetime
    • Plot directly with pandas.DataFrame.plot since the data is in a dataframe
      • This example plots to a secondary_y, otherwise use the parameter logy=True.
    df_binance = pd.DataFrame(binance_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
    df_binance.ts = pd.to_datetime(df_binance.ts, unit='ms')  # convert column to a datetime dtype
    
    df_ftx = pd.DataFrame(ftx_btc_usdt_ohlcv, columns=['ts', 'o', 'h', 'l', 'c', 'v'])
    df_ftx.ts = pd.to_datetime(df_ftx.ts, unit='ms')  # convert column to a datetime dtype
    
    # plot dataframe
    ax = df_binance.plot(x='ts', y='v', label='Binance', figsize=(8, 6))
    p2 = df_ftx.plot(x='ts', y='v', label='FTX', ax=ax, secondary_y=True)
    
    ax.legend(loc='upper left')
    p2.legend(loc='upper right')
    plt.show()
    

    enter image description here

    • Using logy=True instead of secondary_y=True
    # plot dataframe
    ax = df_binance.plot(x='ts', y='v', label='Binance', figsize=(8, 6), logy=True)
    p2 = df_ftx.plot(x='ts', y='v', label='FTX', ax=ax)
    
    ax.legend(bbox_to_anchor=(1, 1.02), loc='upper left')
    plt.show()
    

    enter image description here