Matplotlib——绘图的几个案例_2013-2021年商业银行不良贷款率变化情况_2014-2021年上半年我国商业银行总资产变化情况_2015-2021年我国商业银行净利润变化情况

该博客分享了如何使用Matplotlib绘制三个图表:不良贷款率变化、总资产和净利润走势。展示了2013年至2021年间中国商业银行的关键指标动态,包括不良贷款率的波动、总资产规模的增长以及净利润的增减情况。
摘要由CSDN通过智能技术生成

前段时间有朋友叫我帮忙画几个图用于毕设(图对查重影响不大),征求同意后分享这几个图及绘制使用的Python代码。

一、2013-2021年商业银行不良贷款率变化情况

请添加图片描述

代码:

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

npls=[1,1.25,1.67,1.74,1.74,1.83,1.88,1.84,1.73]
year = [i for i in range(2013, 2022)]

p1 = plt.figure(figsize=(8, 4.944))
p1.add_subplot(111)
bar_width = 0.4  # 设置分组条形的宽度

for x, y in enumerate(npls[0:2], start=2013):
    plt.text(x+0.2, y+0.01, y, ha='left', color="r", alpha=0.7)

for x, y in enumerate(npls[2:8], start=2015):
    plt.text(x+0.08, y+0.028, y, ha='left', color="r", alpha=0.7)

for x, y in enumerate(npls[8:], start=2021):
    plt.text(x, y+0.028, y, ha='left', color="r", alpha=0.7)

plt.plot(year, npls, color='r', marker='o', linestyle='--', alpha=0.7, label='不良贷款率(%)')

plt.ylim(0.9, 2)
plt.ylabel('不良贷款率(%)')

plt.title('2013-2021年我国商业银行不良贷款率变化情况(单位:%)')
plt.xlabel('时间(年)')
plt.legend(loc=2)

plt.show()

二、2014-2021年上半年我国商业银行总资产变化情况

请添加图片描述

代码:

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

total_assets = [130.8, 150.94, 175.94, 190.42, 203.41, 232.34, 265.79, 281.29]
growth_rate = [None, 15.4, 16.56, 8.23, 6.82, 14.22, 14.4, 5.83]
growth_rate2 = [15.4, 16.56, 8.23, 6.82, 14.22, 14.4, 5.83]
year = [i for i in range(2014, 2022)]

p1 = plt.figure(figsize=(8, 4.944))
ax1 = p1.add_subplot(111)
bar_width = 0.4  # 设置分组条形的宽度
ax1.bar(year, total_assets, width=bar_width, color='steelblue', alpha=0.7, label='总资产(万亿元)')
for x, y in enumerate(total_assets, start=2014):
    plt.text(x, y + 5, y, ha='center')
for x, y in enumerate(growth_rate2, start=2015):
    plt.text(x, y * 13.2 + 5, y, ha='left', color="r", alpha=0.7)
ax2 = ax1.twinx()
ax2.plot(year, growth_rate, color='r', marker='o', linestyle='--', alpha=0.7, label='增长率(%)')
# plt.ylim(0, 300)
ax1.set_ylim(0, 330)
ax2.set_ylim(0, 25)
ax1.set_ylabel('总资产(万亿元)')
ax2.set_ylabel('增长率(%)')
plt.title('2014-2021年上半年我国商业银行总资产变化情况(单位:万亿元,%)')
plt.xlabel('时间(年)')
ax1.legend(loc=2)
ax2.legend(loc=1)

plt.show()

三、2015-2021年我国商业银行净利润变化情况

请添加图片描述

代码:

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

net_profits = [1.59, 1.65, 1.75, 1.83, 1.99, 1.94, 2.18]
growth_rate = [None, 3.8, 6.1, 4.6, 8.75, -2.5, 12.4]
growth_rate2 = [3.8, 6.1, 4.6, 8.75, -2.5, 12.4]
year = [i for i in range(2015, 2022)]

p1 = plt.figure(figsize=(8, 4.944))
ax1 = p1.add_subplot(111)
bar_width = 0.4  # 设置分组条形的宽度
ax1.bar(year, net_profits, width=bar_width, color='steelblue', alpha=0.7, label='净利润(万亿元)')
for x, y in enumerate(net_profits, start=2015):
    plt.text(x, y + 0.03, y, ha='center')
for x, y in enumerate(growth_rate2, start=2016):
    plt.text(x + 0.1, y * 0.1 + 0.6, y, ha='left', color="r", alpha=0.7)
ax2 = ax1.twinx()
ax2.plot(year, growth_rate, color='r', marker='o', linestyle='--', alpha=0.8, label='增长率(%)')
# plt.ylim(0, 300)
ax1.set_ylim(0, 2.5)
ax2.set_ylim(-5, 20)
ax1.set_ylabel('净利润(万亿元)')
ax2.set_ylabel('增长率(%)')
plt.title('2015-2021年我国商业银行净利润变化情况(单位:万亿元,%)')
plt.xlabel('时间(年)')
ax1.legend(loc=2)
ax2.legend(loc=1)

plt.show()

以下是Python用matplotlib2013-2023新闻月平均数量情况条形的代码: ```python import matplotlib.pyplot as plt import numpy as np # 月列表 years = list(range(2013, 2024)) months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] year_months = [f"{year}-{month}" for year in years for month in months] # 模拟数据,每个月随机一个数量 news_count = np.random.randint(0, 300, size=len(year_months)) # 计算每每个月的平均数量 year_month_count = {} for year in years: for i, month in enumerate(months): count = np.mean(news_count[(year - 2013) * 12 + i:(year - 2013) * 12 + i + 1]) year_month_count[f"{year}-{month}"] = count # x轴和y轴数据 x = np.arange(len(year_months)) y = np.array([year_month_count[ym] for ym in year_months]) # 绘制条形 plt.bar(x, y) # 设置x轴标签 plt.xticks(x[::12], years, rotation=45) # 设置y轴标签 plt.ylabel("Average Number of News") # 设置标题 plt.title("Average Number of News per Month from 2013 to 2023") # 显示形 plt.show() ``` 解释一下代码: 1. 首先定义了月列表 `years` 和 `months`,以及月字符串的列表 `year_months`,用于后面生成每个月的数据。 2. 接着使用 `numpy.random.randint()` 生成了模拟数据,每个月随机一个数量。 3. 然后计算了每每个月的平均数量,存储在字典 `year_month_count` 中。 4. 接着生成了x轴和y轴的数据,其中x轴是月的序号,y轴是对应的平均新闻数量。 5. 使用 `matplotlib.pyplot.bar()` 函数绘制了条形,并设置了x轴标签、y轴标签和标题。 6. 最后使用 `matplotlib.pyplot.show()` 函数显示形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值