python绘制柱形图呈现用户本金,利息和利息和,横轴为用户姓名,纵轴为金额
时间: 2024-02-27 10:56:16 浏览: 28
好的,您可以使用Python中的matplotlib库来绘制柱形图。以下是一个简单的示例代码,您可以根据您的具体数据进行修改:
```python
import matplotlib.pyplot as plt
# 用户数据
users = ['张三', '李四', '王五']
principal = [1000, 2000, 3000] # 本金
interest = [500, 1000, 1500] # 利息
total = [1500, 3000, 4500] # 本金和利息和
# 绘制柱形图
plt.bar(users, principal, label='本金')
plt.bar(users, interest, bottom=principal, label='利息')
plt.bar(users, total, bottom=[i + j for i, j in zip(principal, interest)], label='本金和利息和')
# 设置图例和标题
plt.legend()
plt.title('用户本金,利息和利息和')
# 显示图形
plt.show()
```
这段代码将会生成一个柱形图,横轴为用户姓名,纵轴为金额,其中每个用户都会有三个柱形,分别代表其本金,利息和本金和利息和。
相关问题
使用Python语言绘制一个柱形图,横轴为用户姓名,纵轴为金额呈现用户的本金,利息和本息和
好的,下面是一个简单的示例代码,使用了Matplotlib库来绘制柱形图:
```python
import matplotlib.pyplot as plt
# 用户数据
user_data = {
'Alice': {'本金': 1000, '利息': 200, '本息和': 1200},
'Bob': {'本金': 800, '利息': 160, '本息和': 960},
'Charlie': {'本金': 1200, '利息': 240, '本息和': 1440},
'David': {'本金': 1500, '利息': 300, '本息和': 1800},
}
# 提取数据
names = list(user_data.keys())
principal = [user_data[name]['本金'] for name in names]
interest = [user_data[name]['利息'] for name in names]
total = [user_data[name]['本息和'] for name in names]
# 绘制柱形图
plt.bar(names, principal, width=0.3, label='本金')
plt.bar([name + 0.3 for name in names], interest, width=0.3, label='利息')
plt.bar([name + 0.6 for name in names], total, width=0.3, label='本息和')
# 添加标题和标签
plt.title('用户资产')
plt.xlabel('用户名')
plt.ylabel('金额')
plt.legend()
# 显示图形
plt.show()
```
在这个示例中,我们创建了一个字典 `user_data` 来存储用户数据。然后,我们使用 `list` 函数提取出用户姓名,并使用字典的方式提取出每个用户的本金、利息和本息和。接着,我们使用 `plt.bar` 函数绘制柱形图,其中每个用户的本金、利息和本息和都用不同的颜色表示。最后,我们添加标题和标签,并使用 `plt.legend` 函数来添加图例。最后,使用 `plt.show` 函数来显示图形。
帮我做一个有关银行柜台储蓄业务处理程序的Python代码,具体要求如下:1.输入用户姓名和本金,输出相应的本息和。2.处理至少五笔业务,并将每次的结果保存到文件中。3.能够输出所有业务清单。4.绘制柱形图,呈现用户的本金、利息和本息和,横轴为用户姓名,纵轴为金额
好的,以下是符合要求的Python代码:
```python
import matplotlib.pyplot as plt
def calculate_interest(principal):
interest_rate = 0.03 # 储蓄利率为3%
interest = principal * interest_rate
return interest
def save_record(name, principal, interest, total):
with open('record.txt', 'a') as f:
f.write(f'{name}\t{principal}\t{interest}\t{total}\n')
def read_records():
with open('record.txt', 'r') as f:
print(f'{"姓名":<6} {"本金":<6} {"利息":<6} {"本息和":<6}')
for line in f.readlines():
name, principal, interest, total = line.strip().split('\t')
print(f'{name:<6} {principal:<6} {interest:<6} {total:<6}')
def draw_bar_chart(names, principals, interests, totals):
plt.bar(names, principals, label='本金')
plt.bar(names, interests, bottom=principals, label='利息')
plt.bar(names, totals, bottom=[p+i for p, i in zip(principals, interests)], label='本息和')
plt.xlabel('姓名')
plt.ylabel('金额')
plt.legend()
plt.show()
names = []
principals = []
interests = []
totals = []
for i in range(5):
name = input('请输入姓名:')
principal = float(input('请输入本金:'))
interest = calculate_interest(principal)
total = principal + interest
save_record(name, principal, interest, total)
names.append(name)
principals.append(principal)
interests.append(interest)
totals.append(total)
read_records()
draw_bar_chart(names, principals, interests, totals)
```
运行该程序后,依次输入五笔业务的用户姓名和本金即可。该程序会自动计算每笔业务的利息和本息和,并将结果保存到文件 `record.txt` 中。在处理完五笔业务后,程序会输出所有业务清单,并绘制柱形图,呈现用户的本金、利息和本息和。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)