【问题标题】:Unable to plot multiple lines in a single graph无法在单个图形中绘制多条线
【发布时间】:2023-03-25 08:40:01
【问题描述】:

我遇到了一个奇怪的问题,我试图在一个图表中绘制多条线,但它只有一条。我正在分享屏幕截图,因为您可以看到两者的接近值不同。它没有渲染 binance 图,因为它似乎被覆盖了。

图表

更新

代码如下

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

【问题讨论】:

    标签: python pandas matplotlib plot binance


    【解决方案1】:
    • 在python 3.8.12、pandas 1.3.3、matplotlib 3.4.3中测试

    现有代码

    • 可以正常工作,但是'Binance' 比'FTX' 小,可以通过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()
    

    • 没有ax.set_yscale('log'),'Binance' 仍会出现在情节中


    • OP 中的文本代码示例使用了'v',但'c' 出现了问题(在屏幕截图中)。
      • 问题是df_ftx.c和df_binance.c几乎一模一样,使用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()
    

    • 鉴于 y 轴的比例,差异太小,无法区分两条线。
    >>> 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
    

    更新代码

    • 使用pd.to_datetime 将'ts' 转换为datetime dtype
    • 直接使用pandas.DataFrame.plot 绘图,因为数据在数据框中
      • 此示例绘制到secondary_y,否则使用参数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()
    

    • 使用logy=True 代替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()
    

    【讨论】:

    • 我会试一试所以这都是由于缩放问题? Close 值看起来非常相似,所以不知道为什么会缩放问题。
    • @Volatil3 使用您现有的代码,即使我不缩放 yaxis,Binance 也会出现。所以我真的不知道为什么它没有显示在你的情节中。我也在 jupyter 中测试它。只要确保您使用的是当前版本的 matplotlib 和 pandas。如果您使用的是 conda,请转到 anaconda 提示符,激活环境(如果您使用的不是base),然后执行conda update --all
    • @Volatil3 问题是df_ftx.c 和df_binance.c 几乎完全相同。注意答案的更新。
    • 我通过pip 安装了两者的更新版本,并且都是最新版本:3.3.4 和 1.3.1。不知道我哪里做错了。但是,我确实在我的 conda 环境中发现了不一致。
    • @Volatil3 请注意,问题是因为两个数据框的c 列中的值几乎相同,因此行重叠
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多