Python画论文级别的柱状图(plt.bar)

Python美化柱状图(plt.bar)

目录

前言

一、核心内容

1. 主体库函数

2. 库函数的使用

3. 美化图片一些代码

二、使用步骤

1.引入库

2.绘图第一步,创建一个画布

3. 系统修改画图的字体、大小和粗细(习惯用Times New Roman和加粗字体'bold')

4.  数据

5. 画图 (两个子图)

6. 利用plt.scatter 和plt. text 打点和标注

三、图片展示

四、完整代码



前言

致力于让小白可以利用python画论文级别的条形图

一、核心内容

1. 主体库函数

matplotlib.pyplot.bar:  条形图垂直与X轴

matplotlib.pyplot.barh: 条形图水平与X轴

官网API:bar(x, height) / barh(y, width) — Matplotlib 3.5.2 documentation

2. 库函数的使用

plt.bar(index_X,data,width=0.1,label='hellow word',color='deepskyblue',hatch='/',zorder=1)  #bar width     hatch=‘//’ 比hatch=‘/’ 斜线密集

plt.barh(index_Y,data,height=0.1,label='How old are you',color='deepskyblue')

index_X (index_Y):plt.bar(plt.barh) 中 画柱状图X(Y)的坐标

data:是你要画的具体值,详细可看下面数据

label:数据的标签 配合plt.legend一起使用

plt.legend(frameon=False,loc='upper right')

color:柱状图上色的颜色,更多颜色可参见:

Matlab RGB 颜色对照表(0-1之间取值)_Anne033的博客-CSDN博客_matlab颜色rgb对照表

hatch:给条形图打上符号,比如hatch='/' 是打上斜杠 ,'//'比'/'斜杠更多哦

zorder:画布绘图先后顺序,顺序不设置的话很容易出现覆盖现象哦

3. 美化图片一些代码

坐标轴加粗

#===坐标轴加粗==
ax=plt.gca();#获得坐标轴的句柄
ax.spines['bottom'].set_linewidth(2);###设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(2);####设置左边坐标轴的粗细
ax.spines['right'].set_linewidth(2);###设置右边坐标轴的粗细
ax.spines['top'].set_linewidth(2);###设置右边坐标轴的粗细

plt.scatter 和plt. text 打点和标注 

二、使用步骤

1.引入库

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib as mpl

2.绘图第一步,创建一个画布

plt.figure(figsize=(25, 15))

3. 系统修改画图的字体、大小和粗细(习惯用Times New Roman和加粗字体'bold')

mpl.rcParams['font.sans-serif'] = ['Times New Roman']  # 设置matplotlib整体用Times New Roman
mpl.rcParams['font.weight'] = 'bold'  # 设置matplotlib整体用Times New Roman
mpl.rcParams['font.size'] = 26  # 设置matplotlib整体用Times New Roman

4.  数据

#图(a)数据
fa1=[0.877,0.867,0.851,0.839,0.502]
fa2=[0.992,0.992,0.983,0.946,0.719]
fa3=[0.99,0.99,0.983,0.719,0.845]

index_fa1=[0.1,0.5,0.9,1.3,1.7]
index_fa2=[0.2,0.6,1.0,1.4,1.8]
index_fa3=[0.3,0.7,1.1,1.5,1.9]
#图(b)数据
fb1=[0.799,0.826,0.815]# 4 7 10
fb2=[0.863,0.893,0.878]
fb3=[0.775,0.799,0.792]
fb4=[0.850,0.877,	0.865]
fb5=[0.77,0.760,0.806]
fb6=[0.847,0.873,0.877]

index_fb1=[1,2,3]
index_fb2=[1.2,2.2,3.2]
index_fb3=[1.1,2.1,3.1]
index_fb4=[1.3,2.3,3.3]

5. 画图 (两个子图)

#==========================子图(a)====================================
plt.subplot(121)
plt.barh(index_fa1,fa1,height=0.1,label='Teemo',color='deepskyblue')
plt.barh(index_fa2,fa2,height=0.1,label='Yasuo',color='mediumturquoise')
plt.barh(index_fa3,fa3,height=0.1,label='Yone',color='g')
plt.legend(frameon=False,loc='upper right')

plt.ylim(0,2.3)
plt.xticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.yticks([0.2,0.6,1.0,1.4,1.8], ['None','SZA', 'SBTS', 'TCWV', 'BTS'], fontsize=26)
plt.ylabel('TA', fontsize=30,fontweight='bold')
plt.xlabel('ZX', fontsize=30,fontweight='bold')
plt.text(0, 2.35, '(b)')
ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)
#==========================子图(b)====================================
plt.subplot(122)
plt.bar(index_fb1,fb1,width=0.1,label='League Of Legend1',color='deepskyblue',zorder=1)  
plt.bar(index_fb2,fb2,width=0.1,label='League Of Legend2',color='deepskyblue',hatch='/',zorder=1)
plt.bar(index_fb3,fb3,width=0.1,label='League Of Legend3',color='mediumturquoise',zorder=1)
plt.bar(index_fb4,fb4,width=0.1,label='League Of Legend4',color='mediumturquoise',hatch='/',zorder=1)
plt.legend(frameon=False)
plt.ylim(0,1.3)
plt.xticks([1.15, 2.15, 3.15], ['April','July', 'October'], fontsize=26)
plt.yticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.xlabel('Test Month', fontsize=30,fontweight='bold')
plt.ylabel('Accuracy', fontsize=30,fontweight='bold')
ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)

6. 利用plt.scatter 和plt. text 打点和标注


plt.scatter(2.2,1.01, s=400, marker="*", color='black',zorder=2)#120
plt.text(2.25, 1, 'MVP')
plt.text(0.8, 1.32, '(a)')

7. 保存图片

plt.savefig('./bar_example.png', bbox_inches='tight', dpi=1200)
plt.show()

bbox_inches='tight':让保存的图片充分充满图片,避免空白很多或者matplotlib图片显示不全(这个小技巧很重要)

dpi:设置图片保存分辨率

三、图片展示

Python画论文级别的柱状图(plt.bar)_第1张图片

四、完整代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib as mpl


plt.figure(figsize=(25, 15))
mpl.rcParams['font.sans-serif'] = ['Times New Roman']  
mpl.rcParams['font.weight'] = 'bold'  
mpl.rcParams['font.size'] = 26  
plt.subplot(121)
fa1=[0.877,0.867,0.851,0.839,0.502]
fa2=[0.992,0.992,0.983,0.946,0.719]
fa3=[0.99,0.99,0.983,0.719,0.845]
index_fa1=[0.1,0.5,0.9,1.3,1.7]
index_fa2=[0.2,0.6,1.0,1.4,1.8]
index_fa3=[0.3,0.7,1.1,1.5,1.9]
plt.barh(index_fa1,fa1,height=0.1,label='Teemo',color='deepskyblue')
plt.barh(index_fa2,fa2,height=0.1,label='Yasuo',color='mediumturquoise')
plt.barh(index_fa3,fa3,height=0.1,label='Yone',color='g')
plt.legend(frameon=False,loc='upper right')
plt.ylim(0,2.3)
plt.xticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.yticks([0.2,0.6,1.0,1.4,1.8], ['None','SZA', 'SBTS', 'TCWV', 'BTS'], fontsize=26)
plt.ylabel('TA', fontsize=30,fontweight='bold')
plt.xlabel('ZX', fontsize=30,fontweight='bold')
plt.text(0, 2.35, '(a)')
ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)

plt.subplot(122)
fb1=[0.799,0.826,0.815]# 4 7 10
fb2=[0.863,0.893,0.878]
fb3=[0.775,0.799,0.792]
fb4=[0.850,0.877,	0.865]
fb5=[0.77,0.760,0.806]
fb6=[0.847,0.873,0.877]
index_fb1=[1,2,3]
index_fb2=[1.2,2.2,3.2]
index_fb3=[1.1,2.1,3.1]
index_fb4=[1.3,2.3,3.3]

plt.bar(index_fb1,fb1,width=0.1,label='League Of Legend1',color='deepskyblue',zorder=1) 
plt.bar(index_fb2,fb2,width=0.1,label='League Of Legend2',color='deepskyblue',hatch='/',zorder=1)
plt.bar(index_fb3,fb3,width=0.1,label='League Of Legend3',color='mediumturquoise',zorder=1)
plt.bar(index_fb4,fb4,width=0.1,label='League Of Legend4',color='mediumturquoise',hatch='/',zorder=1)
plt.legend(frameon=False)
for i in range(3):
    plt.scatter(index_fb3[i], fb5[i], s=400, marker="*", color='black',zorder=2)
    plt.scatter(index_fb4[i], fb6[i], s=400, marker="*", color='black',zorder=2)
plt.ylim(0,1.3)
plt.xticks([1.15, 2.15, 3.15], ['April','July', 'October'], fontsize=26)
plt.yticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.scatter(2.2,1.01, s=400, marker="*", color='black',zorder=2)#120
plt.text(2.25, 1, 'MVP')
plt.text(0.8, 1.32, '(b)')
plt.xlabel('Test Month', fontsize=30,fontweight='bold')
plt.ylabel('Accuracy', fontsize=30,fontweight='bold')
ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)
ax.spines['top'].set_linewidth(2)
plt.savefig('./bar_example.png', bbox_inches='tight', dpi=1200)
plt.show()

你可能感兴趣的:(python,matplotlib)