我有一個奇怪的問題,我試圖在一個圖中繪制多條線,但它只有一條。我正在分享螢屏截圖,因為您可以看到兩者的接近值不同。它沒有呈現幣安圖,因為它似乎被覆寫了。

圖形

更新
代碼如下
# 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()
uj5u.com熱心網友回復:
- 在
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
更新代碼
- 轉換
'ts'為datetime dtype與
- 使用
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337725.html標籤:Python 熊猫 matplotlib 阴谋 币安
- 使用
