pandas - 数据可视化 Python
问题描述
在数据框 open_prices 中为 4 家公司绘制 4 条不同的线图。年份在 X 轴上,股票价格在 Y 轴上,您需要 (2,2) 绘图。将图形大小设置为 10、8 并共享 X 轴以获得更好的可视化效果
import pandas as pd import numpy as np import matplotlib.pyplot as plt from nsepy import get_history import datetime as dt %matplotlib inline start = dt.datetime(2015, 1, 1) end = dt.datetime.today() infy = get_history(symbol='INFY', start = start, end = end) infy.index = pd.to_datetime(infy.index) hdfc = get_history(symbol='HDFC', start = start, end = end) hdfc.index = pd.to_datetime(hdfc.index) reliance = get_history(symbol='RELIANCE', start = start, end = end) reliance.index = pd.to_datetime(reliance.index) wipro = get_history(symbol='WIPRO', start = start, end = end) wipro.index = pd.to_datetime(wipro.index) open_prices = pd.concat([infy['Open'], hdfc['Open'],reliance['Open'], wipro['Open']], axis = 1) open_prices.columns = ['Infy', 'Hdfc', 'Reliance', 'Wipro'] f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) axes[0, 0].plot(open_prices.index.year,open_prices.INFY) axes[0, 1].plot(open_prices.index.year,open_prices.HDB) axes[1, 0].plot(open_prices.index.year,open_prices.TTM) axes[1, 1].plot(open_prices.index.year,open_prices.WIT)
空白图来了。请帮忙....?!??
解决方案
下面的代码工作正常,我改变了以下内容
a) 轴应该是 ax b) DF 列名不正确 c) 任何人都可以尝试这个示例还需要安装 lxml 库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from nsepy import get_history
import datetime as dt
start = dt.datetime(2015, 1, 1)
end = dt.datetime.today()
infy = get_history(symbol='INFY', start = start, end = end)
infy.index = pd.to_datetime(infy.index)
hdfc = get_history(symbol='HDFC', start = start, end = end)
hdfc.index = pd.to_datetime(hdfc.index)
reliance = get_history(symbol='RELIANCE', start = start, end = end)
reliance.index = pd.to_datetime(reliance.index)
wipro = get_history(symbol='WIPRO', start = start, end = end)
wipro.index = pd.to_datetime(wipro.index)
open_prices = pd.concat([infy['Open'], hdfc['Open'],reliance['Open'],
wipro['Open']], axis = 1)
open_prices.columns = ['Infy', 'Hdfc', 'Reliance', 'Wipro']
print(open_prices.columns)
ax=[]
f, ax = plt.subplots(2, 2, sharey=True)
ax[0,0].plot(open_prices.index.year,open_prices.Infy)
ax[1,0].plot(open_prices.index.year,open_prices.Hdfc)
ax[0,1].plot(open_prices.index.year,open_prices.Reliance)
ax[1,1].plot(open_prices.index.year,open_prices.Wipro)
plt.show()
推荐阅读
- c# - 如何从 twilio 跟踪用户消息和用户电话号码到我们的应用程序
- python - 当 2 个列表与 + 运算符连接时会创建多少个列表副本?
- javascript - Javascript自定义HTMLElement问题
- internationalization - 如何使用 Hugo 和 NetlifyCMS 管理国际化
- javascript - 从 XML 标记中获取文本时如何删除空格?
- python - 我在 ubuntu18.04 上执行了 Python 2,但没有导入“tkinter”
- webpack - webpack - 奇怪的控制台消息(提供输出,404 将回退,提供不是来自 webpack 的内容......等等)
- ios - UIImagePicker 在 Objective c 和 Swift 中给出错误的裁剪图像
- email - 带有气流的电子邮件
- php - Codeigniter facebook 身份验证问题