Skip to main content
 首页 » 编程设计

Python:使用 Datareader 截断列宽

2024年12月31日7dudu

我正在设置一个包含股票价格和交易量的matplotlib图表。该图将从 pandas datareader 获取数据。

我的问题是数量与价格相比非常高,因此它们的图表看起来非常不成比例,而且由于库存不断变化,我无法缩放它。

我如何截断每列的宽度,而不是说每行显示“10”位数字而只显示“5”?。

我试图将每一列分隔成一个字符串并使用拆分,但我的问题是找到一种方法将它们追加回数据框。

-- 旁注:如果有一种方法可以将成交量作为条形图,而价格是一条线,那就太棒了。

start = datetime.date(2017, 12, 29) 
end = datetime.date(2018, 12, 29) 
f = web.DataReader('GOOGL', 'iex', start, end) 
 
 
f = f.head[10] 
price = f.open 
volume = f.volume 
time = f.shape[0] 
 
plt.plot(time,price) 
plt.plot(time, volume) 
plt.show() 

请您参考如下方法:

您可以设置不同刻度的双轴。

start = datetime.date(2017, 12, 29) 
end = datetime.date(2018, 12, 29) 
import pandas_datareader.data as web 
f = web.DataReader('GOOGL', 'iex', start, end)    
f = f.head() 
 
fig, ax1 = plt.subplots() 
ax1.bar(x = f.index, height = f.volume/1000, width = 0.3) 
ax1.set_ylabel('volumn in 1000s', color='b') 
ax1.tick_params('y', colors='b') 
 
ax2 = ax1.twinx() 
ax2.plot(f.index, f.open, 'g') 
ax2.set_ylabel('open price', color='g') 
ax2.tick_params('y', colors='g') 
 
fig.tight_layout() 
plt.show()