- 在
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()
>>> 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
更新代码
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()