Matplotlib——绘图的几个案例

Matplotlib——绘图的几个案例,第1张

Matplotlib绘图的几个案例
      • 一、2013-2021年商业银行不良贷款率变化情况
      • 二、2014-2021年上半年我国商业银行总资产变化情况
      • 三、2015-2021年我国商业银行净利润变化情况

前段时间有朋友叫我帮忙画几个图用于毕设(图对查重影响不大),征求同意后分享这几个图及绘制使用的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()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/734336.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存