柱状图和折线图放在一张图上展示matplotlib.pyplot -- 详细步骤

目录

准备调用的包:

准备数据:

准备图:

画柱状图

画折线图:

显示图例:

配置坐标边缘以及图的合适布局

展示和保存图

全部代码展示:


图像展示:

准备调用的包:

import numpy as np
import matplotlib.pyplot as plt

准备数据:

# 柱状图的值
acc = [95.95,95.48,95.76,95.95,95.48,95.76,95.95,95.48,95.76,96.89]
# 折线图的值
f1 = [95.95,95.48,95.76,95.95,95.48,95.76,95.95,95.48,95.76,96.89]
# X轴显示的值
x = ['10%','20%','30%','40%','50%','60%','70%','80%','90%','100%']

准备将X轴的值放在图对应的坐标位置:
 

# 坐标位置
x_len = np.arange(len(x))

准备图:

# 创建图像大小
plt.figure(figsize=(25, 8))
#  创建坐标轴,用变量ax表示一个坐标轴实例
ax = plt.axes()
# 也可以通过fig,ax=plt.subplots()命令一次性建立图像和坐标轴

# 为图像添加虚线
plt.grid(axis="y", c='#d2c9eb', linestyle = '--',zorder=0)

画柱状图

# 柱子的一些配置: 柱子颜色,柱子宽度,柱子边缘线的颜色,宽度
plt.bar(x=x, height=acc, label='Acc', color='#92a6be', alpha=0.8,linewidth = 2,edgecolor='black')
# 柱子的横坐标设置(以及横坐标值)
plt.xticks(x_len, x, fontproperties='Times New Roman',fontsize = 40)
# 柱子纵坐标设置以及纵坐标值
plt.yticks(fontproperties='Times New Roman',fontsize = 40)
plt.ylim(ymin=92)
# 横坐标的标题
plt.xlabel("Data Percentage", fontproperties='Times New Roman',fontsize=45)
# 纵坐标标题
plt.ylabel("Cresci-15",fontproperties='Times New Roman', fontsize=45)

画折线图:

# 配置折线
plt.plot(x, f1, "r", marker='o', c='#f27628', ms=10, linewidth='3', label="f1")
# 显示数字(看需求)
for a, b in zip(x, f1):
    plt.text(a, b + 0.3, b, ha='center', va='bottom', fontsize=35)
# b+0.3 是向上0.3的位置写数字

显示图例:

plt.legend(ncol=2,prop={'family' : 'Times New Roman', 'size': 35})

配置坐标边缘以及图的合适布局

# 配置坐标边缘线
ax.spines['bottom'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['bottom'].set_color('black')
ax.spines['top'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['top'].set_color('black')
ax.spines['right'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['right'].set_color('black')
ax.spines['left'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['left'].set_color('black')
# 调整图的布局,tight_layout会自动调整子图参数,使之填充整个图像区域
plt.tight_layout()

展示和保存图

plt.show()
plt.savefig("column_scale.png",dpi=300)

全部代码展示:

import json
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
acc = [95.95,95.48,95.76,95.95,95.48,95.76,95.95,95.48,95.76,96.89]
f1 = [95.95,95.48,95.76,95.95,95.48,95.76,95.95,95.48,95.76,96.89]
x = ['10%','20%','30%','40%','50%','60%','70%','80%','90%','100%']
x_len = np.arange(len(x))
plt.figure(figsize=(25, 8))
ax = plt.axes()
plt.grid(axis="y", c='#d2c9eb', linestyle = '--',zorder=0)


plt.bar(x=x, height=acc, label='Acc', color='#92a6be', alpha=0.8,linewidth = 2,edgecolor='black')
plt.xticks(x_len, x, fontproperties='Times New Roman',fontsize = 40)
plt.yticks(fontproperties='Times New Roman',fontsize = 40)
plt.ylim(ymin=92)
plt.xlabel("Data Percentage", fontproperties='Times New Roman',fontsize=45)
plt.ylabel("Cresci-15",fontproperties='Times New Roman', fontsize=45)
plt.plot(x, f1, "r", marker='o', c='#f27628', ms=10, linewidth='3', label="f1")
# 显示数字
for a, b in zip(x, f1):
    plt.text(a, b + 0.3, b, ha='center', va='bottom', fontsize=35)

plt.legend(ncol=2,prop={'family' : 'Times New Roman', 'size': 35})
# plt.legend(prop={'family' : 'Times New Roman', 'size': 35}, ncol = 2)

ax.spines['bottom'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['bottom'].set_color('black')
ax.spines['top'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['top'].set_color('black')
ax.spines['right'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['right'].set_color('black')
ax.spines['left'].set_linewidth('2.0')#设置边框线宽为2.0
ax.spines['left'].set_color('black')
plt.tight_layout()
plt.show()
plt.savefig("column_scale.png",dpi=300)
 

猜你喜欢

转载自blog.csdn.net/qq_40671063/article/details/130644924