python - 如何在 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 的变化)作为条形的行使价。
解决方案
IIUC,这就是我的做法。这应该有一个带“strikePrice”的 x 轴和两个“changeinOpenInterest”和“openInterest”条。
disp_df = df.pivot('strikePrice', 'changeinOpenInterest', 'openInterest')
disp_df.plot(kind='bar')
您可以在情节中添加您想要的花里胡哨,但这避免了您在上面所做的很多操作。
推荐阅读
- java - SOAP 客户端无法使用双向 tls 进行身份验证
- java - 我在 bean factory 中遇到的这个错误的解决方案是什么?
- python - 值误差,使用VGG16的图像归一化误差,人脸识别和检测项目
- python-3.x - 无法初始化到 serial@/dev/ttyUSB0:telosb 的串行连接
- reactjs - 使用带有反应的 Wordpress 联系表格 7
- javascript - Selenium Python,如何让 execute_async_script() 返回 true 或 false
- postgresql - pgbackrest 多主机服务器
- twig - 如何区分这两个值?Twig 返回 sql 组 concat
- python - 在 azure synapse 中存储一个简单的字符串作为文本文件(到数据湖 gen2)
- sql - 在 Snowflake 和 SQL 中合并完整和不完整的数据帧