首页 > 解决方案 > 用 x 值范围填充 matplotlib 上的 x 和 y 值

问题描述

我正在使用熊猫和 matplotlib。使用某种颜色,我想填充我的 x 轴和价格(又名收盘价)之间的空间。我该怎么办?我是python的初学者。

import datetime as dt
import pandas_datareader.data as web

import matplotlib.pyplot as plt
import pandas as pd


# Defining Axis
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0)) 


# Data Section
start = dt.datetime(2017,1,1)
end   = dt.datetime(2018,1,1)


sp500 = web.DataReader("TSLA", "yahoo", start, end)
close = sp500['Adj Close']



# Axis Settings Section
ax1.fill_between(start, close, 0, color='grey')  #here is where I'm having issues. I think the issues im having partly involve the fact that im dealing with a range of dates
ax1.plot(close, label = 'price')
ax1.grid(True, color= 'lightgreen', linestyle= '-')
ax1.xaxis.label.set_color('dodgerblue')
ax1.yaxis.label.set_color('indianred')
ax1.set_yticks([0,100,200,300,400])                 




# Graph/Chart Settings
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.xticks(rotation=45)
plt.subplots_adjust(left=0.09, bottom=0.20, top=0.90, wspace=0.20, hspace=0.0)
plt.show()

标签: pythonmatplotlib

解决方案


尝试这个。您需要一组 x 值,而不仅仅是“开始”中的一个标量值:

ax1.fill_between(sp500.index, close, 0, color='grey')  

输出:

在此处输入图像描述


推荐阅读