python - 在 1 个图中添加 2 个条形列和 1 个线列
问题描述
我有一个df_daily
我想要有 2 列的地方daily_sentiment_polarity
,并daily_sentiment_subjectivity
作为绘图上的条形和 1 列close
作为绘图中的一条线。x 轴应该是变量date
。
我尝试了以下方法:
df_daily.plot(kind='bar', x='date', y='daily_sentiment_polarity')
df_daily.plot(kind='bar', x='date', y='daily_sentiment_subjectivity')
df_daily.plot(kind='line', x='date', y='close')
但这导致了 3 个单独的地块。有人可以帮我将这 3 个合并为 1 个情节吗?
解决方案
您可以尝试以下方法:
import pandas as pd
import matplotlib.pyplot as plt
data={"date":["01-05-2021","02-05-2021","03-05-2021"],
'daily_sentiment_polarity':[0,1,0],
'daily_sentiment_subjectivity':[1,1,0],
'close':[10,20,5]
}
df_daily=pd.DataFrame(data)
ax=df_daily.plot(kind='bar', x='date', y=['daily_sentiment_polarity','daily_sentiment_subjectivity'])
df_daily.plot(kind='line', x='date', y='close',ax=ax, color='r',secondary_y=True)
plt.show()
推荐阅读
- c# - 平移形状后不会围绕它的原点旋转
- json - Dart Http Client 返回截断的响应,当我打印到控制台时截断略有不同
- python - 如何在python中检测“之后的一组字符串
- node.js - .npmrc 中以双斜杠“//”开头的行是什么意思?
- excel - Excel VBA - 过滤动态范围内的行
- javascript - 仅使用 chrome.storage.local.set 更新指定元素
- c++ - 计算 FreeType 字形渲染的位置?
- javascript - 使用 JavaScript 将数据写入文本文件
- azure-data-explorer - 查找多个字段组合的最大计数
- powerbi - 面向列的矩阵中的一般每月总计