[英]Unable to plot multiple lines in a single graph
我有一个奇怪的问题,我试图在一个图中绘制多条线,但它只有一条。 我正在分享屏幕截图,因为您可以看到两者的接近值不同。 它没有呈现币安图,因为它似乎被覆盖了。
图形
更新
代码如下
# 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 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'
仍然出现在情节上'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
pd.to_datetime
将'ts'
转换为datetime dtype
pd.to_datetime
pandas.DataFrame.plot
绘图,因为数据在数据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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.