尝试在 Python 中使用 Seaborn 制作两侧条形图,但它似乎没有为图的一侧使用正确的级别。
数据如下所示:
Time Symbol Position Operation Side Price Size
0 2023-07-25 15:09:12.249964 MCDU3 0 0 1 0.7595 -2
1 2023-07-25 15:09:12.255196 MCDU3 1 0 1 0.7594 -7
2 2023-07-25 15:09:12.258575 MCDU3 2 0 1 0.7593 -8
3 2023-07-25 15:09:12.267100 MCDU3 3 0 1 0.7592 -16
4 2023-07-25 15:09:12.270027 MCDU3 4 0 1 0.7591 -14
5 2023-07-25 15:09:12.272276 MCDU3 5 0 1 0.759 -407
6 2023-07-25 15:09:12.274441 MCDU3 6 0 1 0.7589 -14
7 2023-07-25 15:09:12.276581 MCDU3 7 0 1 0.7588 -14
8 2023-07-25 15:09:12.278742 MCDU3 8 0 1 0.7587 -264
9 2023-07-25 15:09:12.280768 MCDU3 9 0 1 0.7586 -15
10 2023-07-25 15:09:12.283094 MCDU3 0 0 0 0.7596 102
11 2023-07-25 15:09:12.286398 MCDU3 1 0 0 0.7597 8
12 2023-07-25 15:09:12.289751 MCDU3 2 0 0 0.7598 8
13 2023-07-25 15:09:12.292842 MCDU3 3 0 0 0.7599 17
14 2023-07-25 15:09:12.295488 MCDU3 4 0 0 0.76 409
15 2023-07-25 15:09:12.297606 MCDU3 5 0 0 0.7601 16
16 2023-07-25 15:09:12.299546 MCDU3 6 0 0 0.7602 16
17 2023-07-25 15:09:12.302073 MCDU3 7 0 0 0.7603 14
18 2023-07-25 15:09:12.305483 MCDU3 8 0 0 0.7604 14
19 2023-07-25 15:09:12.307733 MCDU3 9 0 0 0.7605 658
代码看起来像这样——我不清楚为什么使用第一个
Price
的Side
级别而不是第二个图的实际Price
。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
f, ax = plt.subplots()
sns.set_color_codes('muted')
# d.loc[d.Side==1,'Size'] = d[d.Side==1].Size*-1
sns.barplot(data = d[d.Side==1], x = 'Size', y = 'Price', color = 'b', orient = 'h')
sns.barplot(data = d[d.Side==0], x = 'Size', y = 'Price', color = 'r', orient = 'h')
默认情况下,条形位置是分类的(内部编号为 0、1、2...,然后获取字符串标签)。新的
native_scale
参数给出了数字位置。条形的宽度取决于最接近的条形位置。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.barplot(data=d[d.Side == 1], x='Size', y='Price', color='b', orient='h', native_scale=True)
sns.barplot(data=d[d.Side == 0], x='Size', y='Price', color='r', orient='h', native_scale=True)
plt.show()