怎么用tushare获取浦发银行过去股价
时间: 2023-08-17 12:08:17 浏览: 55
我可以帮你解答这个问题。要使用Tushare获取浦发银行过去的股价,你需要安装并导入Tushare库,并获取一个有效的token。以下是获取浦发银行过去股价的示例代码:
```python
import tushare as ts
# 设置token
ts.set_token('your_token')
# 初始化pro接口
pro = ts.pro_api()
# 获取浦发银行股票代码
df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name')
# 过滤出浦发银行股票代码
pufa_code = df[df['name'].str.contains('浦发银行')]['ts_code'].values[0]
# 获取浦发银行过去股价
df = pro.daily(ts_code=pufa_code, start_date='20100101', end_date='20211231')
# 打印出浦发银行过去股价数据
print(df)
```
请确保你已经安装了Tushare库,并将上述代码中的`your_token`替换为你自己的Tushare token。这样你就可以获取到浦发银行过去的股价数据了。
相关问题
通过tushare获取数据,使用上述数据绘制浦发银行股票成交量的时间序列图
好的,首先需要安装tushare库,可以使用以下命令进行安装:
```
!pip install tushare
```
安装完成后,我们可以使用以下代码获取浦发银行股票的成交量数据:
```python
import tushare as ts
# 设置token
ts.set_token('your_token_here')
# 初始化pro接口
pro = ts.pro_api()
# 获取浦发银行股票的成交量数据
df = pro.daily(ts_code='600000.SH', start_date='20200101', end_date='20211231', fields='trade_date,vol')
# 将成交量数据按照日期排序
df = df.sort_values('trade_date')
# 将日期转换为datetime类型
df['trade_date'] = pd.to_datetime(df['trade_date'])
# 将日期设置为数据的索引
df.set_index('trade_date', inplace=True)
```
接下来,我们可以使用matplotlib库绘制时间序列图:
```python
import matplotlib.pyplot as plt
# 绘制时间序列图
plt.plot(df.index, df['vol'])
# 设置x轴标签
plt.xlabel('Date')
# 设置y轴标签
plt.ylabel('Volume')
# 设置图形标题
plt.title('Pudong Development Bank Stock Volume')
# 显示图形
plt.show()
```
注意:在上述代码中,需要将`your_token_here`替换为你在tushare官网上申请的token。
完整代码如下:
通过tushare获取数据,使用上述数据绘制浦发银行股票成交量的时间序列的南丁格尔玫瑰图
首先需要导入相关的库,包括tushare、matplotlib和numpy:
```python
import tushare as ts
import matplotlib.pyplot as plt
import numpy as np
```
然后使用tushare获取浦发银行股票的交易数据:
```python
df = ts.get_hist_data('600000')
```
接下来对数据进行处理,提取成交量数据,并将其分组成若干个区间。这里我们使用numpy的histogram函数来实现:
```python
data = df['volume'].values
bins = np.histogram_bin_edges(data, bins=18)
hist, _ = np.histogram(data, bins=bins)
```
然后根据南丁格尔玫瑰图的定义,将每个区间的数值转换为极径长度,并计算每个区间的角度:
```python
theta = np.linspace(0, 2*np.pi, len(hist)+1)
radii = np.sqrt(hist / hist.sum())
```
最后使用matplotlib绘制南丁格尔玫瑰图:
```python
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='polar')
ax.bar(theta[:-1], radii, width=(theta[1]-theta[0]), alpha=0.5)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_xticklabels(['0', '2.5', '5.0', '7.5', '10.0', '12.5', '15.0', '17.5'])
plt.show()
```
完整代码如下:
```python
import tushare as ts
import matplotlib.pyplot as plt
import numpy as np
df = ts.get_hist_data('600000')
data = df['volume'].values
bins = np.histogram_bin_edges(data, bins=18)
hist, _ = np.histogram(data, bins=bins)
theta = np.linspace(0, 2*np.pi, len(hist)+1)
radii = np.sqrt(hist / hist.sum())
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='polar')
ax.bar(theta[:-1], radii, width=(theta[1]-theta[0]), alpha=0.5)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_xticklabels(['0', '2.5', '5.0', '7.5', '10.0', '12.5', '15.0', '17.5'])
plt.show()
```