首页 > 解决方案 > 数据可视化 Python

问题描述

  1. 在数据框 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)
    

空白图来了。请帮忙....?!??

标签: pandas

解决方案


下面的代码工作正常,我改变了以下内容

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()

推荐阅读