首页 > 解决方案 > 如何在 plot.bar() 中绘制 3 个或更多值

问题描述

我尝试使用将它们放在列表中的 2 个值来制作 plot.bar(),但我无法绘制 3 个值。

我尝试添加 plot.bar(x,y,z),但没有成功。

ce_data = ce_data.drop(
            ['pchangeinOpenInterest', 'totalTradedVolume', 'impliedVolatility',  # this removes unecesssary items
             'pChange', 'totalBuyQuantity', 'totalSellQuantity', 'bidQty',
             'bidprice', 'askQty', 'askPrice', 'askQty', 'identifier', 'lastPrice', 'change', 'expiryDate',
             'underlying'], axis=1)[
            ['openInterest', 'changeinOpenInterest', 'strikePrice', 'underlyingValue']]

        style.use('ggplot')
        ce_data.to_csv('kumar.csv')

        df = pd.read_csv('kumar.csv', parse_dates=True, index_col=0)
        pivot = df.iloc[2, 3]  # this selects the strike price
        pivot_round = round(pivot, -2)  # round of the price

x = df['strikePrice'].tolist()
y = df['changeinOpenInterest'].tolist()
z = df['openInterest'].tolist()

for i in range(len(x)):
    if int(x[i]) >= pivot_round - 400:
        xleftpos = i
        break

for i in range(len(x)):
    if int(x[i]) >= pivot_round + 400:
        xrightpos = i
        break

x = x[xleftpos:xrightpos]
y = y[xleftpos:xrightpos]
z = z[xleftpos:xrightpos]

plot.bar([value for value in range(len(x))],y)

plot.set_xticks([idx + 0.5 for idx in range(len(x))])
plot.set_xticklabels(x, rotation=35, ha='right', size=10)

我期望 x 轴和 y 和 z(oi 和 oi 的变化)作为条形的行使价。

标签: pythonpandasmatplotlib

解决方案


IIUC,这就是我的做法。这应该有一个带“strikePrice”的 x 轴和两个“changeinOpenInterest”和“openInterest”条。

disp_df = df.pivot('strikePrice', 'changeinOpenInterest', 'openInterest')
disp_df.plot(kind='bar')

您可以在情节中添加您想要的花里胡哨,但这避免了您在上面所做的很多操作。


推荐阅读