用Matplotlib绘图,以证券简称为x轴,最新价为y轴使用条形图展示,将生成的条形图图以 shares_bar. png 为文件名保存在桌面上
时间: 2024-02-09 18:09:49 浏览: 188
好的,我可以为您提供代码来实现这个功能。请确保已经安装了Matplotlib库。
```python
import matplotlib.pyplot as plt
import pandas as pd
# 读取股票数据
data = pd.read_csv('stocks.csv')
# 选择需要绘制的数据
x = data['简称']
y = data['最新价']
# 绘制条形图
plt.bar(x, y)
# 设置图形标题和轴标签
plt.title('最新股票价格')
plt.xlabel('股票简称')
plt.ylabel('最新价')
# 保存图形
plt.savefig('C:/Users/用户名/Desktop/shares_bar.png')
# 显示图形
plt.show()
```
在上面的代码中,我们使用了pandas库来读取股票数据,然后选择需要绘制的数据。接着使用Matplotlib库来绘制条形图,设置图形标题和轴标签。最后使用`savefig()`函数将生成的图形保存在桌面上。请将代码中的文件路径替换为您的桌面路径。
相关问题
证券代码 证券简称 最新价 涨跌幅% 000609 中迪投资 4.80 10.09 000993 闽东电力 4.80 10.09 002615 哈尔斯 5.02 10.09 000795 英洛华 3.93 10.08 002766 索菱股份 6.78 10.06 000971 高升控股 3.72 10.06 000633 合金投资 4.60 10.06 300173 智慧松德 4.60 10.05 300279 和晶科技 5.81 10.05 000831 五矿稀土 9.87 10.04 (1)使用DataFrame创建该股票数据;(8分) (2)请用Matplotlib绘图,以证券简称为x轴,最新价为y轴使用条形图展示,将生成的条形图图以 shares_bar. png 为文件名保存在桌面上。(12分)
(1)使用DataFrame创建该股票数据:
```python
import pandas as pd
data = {'证券代码': ['000609', '000993', '002615', '000795', '002766', '000971', '000633', '300173', '300279', '000831'],
'证券简称': ['中迪投资', '闽东电力', '哈尔斯', '英洛华', '索菱股份', '高升控股', '合金投资', '智慧松德', '和晶科技', '五矿稀土'],
'最新价': [4.80, 4.80, 5.02, 3.93, 6.78, 3.72, 4.60, 4.60, 5.81, 9.87],
'涨跌幅%': [10.09, 10.09, 10.09, 10.08, 10.06, 10.06, 10.06, 10.05, 10.05, 10.04]}
df = pd.DataFrame(data)
print(df)
```
输出结果:
```
证券代码 证券简称 最新价 涨跌幅%
0 000609 中迪投资 4.80 10.09
1 000993 闽东电力 4.80 10.09
2 002615 哈尔斯 5.02 10.09
3 000795 英洛华 3.93 10.08
4 002766 索菱股份 6.78 10.06
5 000971 高升控股 3.72 10.06
6 000633 合金投资 4.60 10.06
7 300173 智慧松德 4.60 10.05
8 300279 和晶科技 5.81 10.05
9 000831 五矿稀土 9.87 10.04
```
(2)请用Matplotlib绘图,以证券简称为x轴,最新价为y轴使用条形图展示,将生成的条形图图以 shares_bar. png 为文件名保存在桌面上。
```python
import matplotlib.pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
# 绘制条形图
plt.bar(df['证券简称'], df['最新价'])
# 添加标题和标签
plt.title('股票最新价')
plt.xlabel('证券简称')
plt.ylabel('最新价')
# 保存图像
plt.savefig('C:\\Users\\Desktop\\shares_bar.png')
# 显示图像
plt.show()
```
输出结果:
![image.png](attachment:image.png)
4.现有如图所示的股票数据,根据图中的数据,完成以下需求: (1)使用DataFrame创建该股票数据。 (2)以证券简称为x轴,最新价为y轴使用条形图展示,将生成的条形图图以shares_bar.png为文件名保存
首先,我们需要理解你提供的"图"的具体内容,由于这是一个文字环境,我无法直接查看图片。假设你是想用Python的pandas和matplotlib库来处理这个任务。让我们基于常见的CSV文件格式来描述这个过程,其中包含两列:日期(Date)和每只股票的最新价格(Price),以及证券简称(Ticker)。
```python
import pandas as pd
import matplotlib.pyplot as plt
# 假设股票数据存放在名为stock_data.csv的CSV文件中
# 其中第一列为日期,第二列为股票简称,第三列为最新价
df_stock = pd.read_csv('stock_data.csv')
# 创建DataFrame
stock_df = pd.DataFrame(df_stock, columns=['Date', 'Ticker', 'Price'])
# 确保我们要画图的是最新的价格,可能需要按日期排序数据
stock_df = stock_df.sort_values('Date', ascending=False)
# 将Ticker列转换为索引,以便我们可以轻松地用Ticker作为x轴
stock_df.set_index('Ticker', inplace=True)
# 条形图
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(stock_df.index, stock_df['Price'], color='blue')
plt.xlabel('证券简称 (Ticker)')
plt.ylabel('最新价')
plt.title('股票最新价条形图')
plt.savefig('shares_bar.png')
```
这段代码首先从CSV文件读取数据,创建DataFrame,然后按日期降序排列,将证券简称设置为行索引。最后,使用matplotlib绘制条形图,并将其保存为'shares_bar.png'。
如果你能提供具体的图数据格式或文件,我可以给出更准确的帮助。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)