文字图例尽眉目
教程来源: https://datawhalechina.github.io/fantastic-matplotlib

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
import datetime
1、Figure和Axes上的文本
Matplotlib具有广泛的文本支持,包括对数学表达式的支持、对栅格和矢量输出的TrueType支持、具有任意旋转的换行分隔文本以及Unicode支持。
1.1、文本API示例
下面的命令是介绍了通过pyplot API和objected-oriented API(面向对象,例如Axes),分别创建文本的方式。
# 通过一个综合例子,以OO模式展示这些API是如何控制一个图像中各部分的文本
fig = plt.figure()
ax = fig.add_subplot()
# 分别为figure和ax设置标题,注意两者的位置是不同的
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax.set_title('axes title')
# 设置x和y轴标签
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
# 设置x和y轴显示范围均为0到10
ax.axis([0, 10, 0, 10])
# 在子图上添加文本
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})
# facecolor:填充颜色,alpha:调整饱和度,pad:设置边框
# 在画布上添加文本,一般在子图上添加文本是更常见的操作,这种方法很少用
fig.text(0.4,0.8,'This is text for figure')
ax.plot([2], [1], 'o')
# 添加注解
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),arrowprops=dict(facecolor='black', shrink=0.05));
1.2、text - 子图上的文本
text的调用方式为Axes.text(x, y, s, fontdict=None, **kwargs)
其中x,y为文本出现的位置,默认状态下即为当前坐标系下的坐标值,
s为文本的内容,
fontdict是可选参数,用于覆盖默认的文本属性,
**kwargs为关键字参数,也可以用于传入文本样式参数
重点解释下fontdict和**kwargs参数,这两种方式都可以用于调整呈现的文本样式,最终效果是一样的,
不仅text方法,其他文本方法如set_xlabel,set_title等同样适用这两种方式修改样式。
通过一个例子演示这两种方法是如何使用的。
fig = plt.figure(figsize=(10,3))
axes = fig.subplots(1,2)
# 使用关键字参数修改文本样式
axes[0].text(0.3, 0.8, 'modify by **kwargs', style='italic',
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10});
# 使用fontdict参数修改文本样式
font = {'bbox':{'facecolor': 'red', 'alpha': 0.5, 'pad': 10}, 'style':'italic'}
axes[1].text(0.3, 0.8, 'modify by fontdict', fontdict=font);
matplotlib中所有支持的样式参数官网文档:
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text
1.3、xlabel和ylabel - 子图的x,y轴标签
xlabel的调用方式为Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, kwargs)
其中xlabel即为标签内容,
fontdict和kwargs用来修改样式,上一小节已介绍,
labelpad为标签和坐标轴的距离,默认为4,
loc为标签位置,可选的值为’left’, ‘center’, 'right’之一,默认为居中
# 观察labelpad和loc参数的使用效果
fig = plt.figure(figsize=(10,3))
axes = fig.subplots(1,2)
axes[0].set_xlabel('xlabel',labelpad=30,loc='left')
# loc参数仅能提供粗略的位置调整,如果想要更精确的设置标签的位置,可以使用position参数+horizontalalignment参数来定位
# position由一个元组过程,第一个元素0.2表示x轴标签在x轴的位置,第二个元素对于xlabel其实是无意义的,随便填一个数都可以
# horizontalalignment='left'表示左对齐,这样设置后x轴标签就能精确定位在x=0.2的位置处
axes[1].set_xlabel('xlabel', position=(0.2, _), horizontalalignment='right');
1.4、title和suptitle - 子图和画布的标题
title的调用方式为Axes.set_title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)
其中label为子图标签的内容,fontdict,loc,**kwargs和之前小节相同不重复介绍
pad是指标题偏离图表顶部的距离,默认为6
y是title所在子图垂向的位置。默认值为1,即title位于子图的顶部。
suptitle的调用方式为figure.suptitle(t, **kwargs)
其中t为画布的标题内容
# 观察pad参数的使用效果
fig = plt.figure(figsize=(10,3))
fig.suptitle('This is figure title',y=1.2) # 通过参数y设置高度
axes = fig.subplots(1,2)
axes[0].set_title('This is title',loc='right',pad=30)
axes[1].set_title('This is title',loc='left',pad=6);
1.5、annotate - 子图的注解
ax.annotate(“Annotation”,
xy=(x1, y1), xycoords=‘data’,
xytext=(x2, y2), textcoords=‘offset points’,
)
annotate的调用方式为Axes.annotate(text, xy, *args, **kwargs)
annotation绘制一个连接坐标轴上两点的箭头:
这将在给定坐标(xycoords)的xy上用textcods中给定的xytext注释一个点。
通常,标注点在数据坐标中指定,而标注文本在偏移点中指定。
通过指定arrowprops参数,可以选择绘制连接xy到xytext的箭头。
若要只绘制箭头,请使用空字符串作为第一个参数。
其中text为注解的内容,
xy为注解箭头指向的坐标,
其他常用的参数包括:
xytext为注解文字的坐标,
xycoords用来定义xy参数的坐标系,
textcoords用来定义xytext参数的坐标系,
arrowprops用来定义指向箭头的样式
annotate的参数非常复杂,这里仅仅展示一个简单的例子,
更多参数可以查看官方文档中的annotate介绍:
https://matplotlib.org/stable/tutorials/text/annotations.html#plotting-guide-annotation
fig = plt.figure()
ax = fig.add_subplot()
ax.annotate("",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.3") #rad 修改折线曲率
);
arrowstyle线条标记如下表:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(3, 3))
ax.annotate("Test",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, va="center", ha="center",
arrowprops=dict(arrowstyle="simple", # 备选fancy simple wedge
connectionstyle="arc3,rad=-0.2"),
)
plt.show()
import matplotlib.pyplot as plt
def demo_con_style(ax, connectionstyle):
x1, y1 = 0.3, 0.2
x2, y2 = 0.8, 0.6
ax.plot([x1, x2], [y1, y2], ".")
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle=connectionstyle,
),
)
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
transform=ax.transAxes, ha="left", va="top")
fig, axs = plt.subplots(3, 5, figsize=(7, 6.3), constrained_layout=True)
demo_con_style(axs[0, 0], "angle3,angleA=90,angleB=0")
demo_con_style(axs[1, 0], "angle3,angleA=0,angleB=90")
demo_con_style(axs[0, 1], "arc3,rad=0.")
demo_con_style(axs[1, 1], "arc3,rad=0.3")
demo_con_style(axs[2, 1], "arc3,rad=-0.3")
demo_con_style(axs[0, 2], "angle,angleA=-90,angleB=180,rad=0")
demo_con_style(axs[1, 2], "angle,angleA=-90,angleB=180,rad=5")
demo_con_style(axs[2, 2], "angle,angleA=-90,angleB=10,rad=5")
demo_con_style(axs[0, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=0")
demo_con_style(axs[1, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=5")
demo_con_style(axs[2, 3], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
demo_con_style(axs[0, 4], "bar,fraction=0.3")
demo_con_style(axs[1, 4], "bar,fraction=-0.3")
demo_con_style(axs[2, 4], "bar,angle=180,fraction=-0.2")
for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1.25), xticks=[], yticks=[], aspect=1.25)
fig.set_constrained_layout_pads(wspace=0, hspace=0, w_pad=0, h_pad=0)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, ax = plt.subplots(figsize=(5, 5))
ax.set_aspect(1)
x1 = -1 + np.random.randn(100)
y1 = -1 + np.random.randn(100)
x2 = 1. + np.random.randn(100)
y2 = 1. + np.random.randn(100)
ax.scatter(x1, y1, color="r")
ax.scatter(x2, y2, color="g")
bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
ax.text(-2, -2, "Sample A", ha="center", va="center", size=20,
bbox=bbox_props)
ax.text(2, 2, "Sample B", ha="center", va="center", size=20,
bbox=bbox_props)
bbox_props = dict(boxstyle="rarrow", fc=(0.8, 0.9, 0.9), ec="b", lw=2)
t = ax.text(0, 0, "Direction", ha="center", va="center", rotation=45,
size=15,
bbox=bbox_props)
bb = t.get_bbox_patch()
bb.set_boxstyle("rarrow", pad=0.6)
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
plt.show()
1.6、字体的属性设置
字体设置一般有全局字体设置和自定义局部字体设置两种方法:
为方便在图中加入合适的字体,可以尝试了解中文字体的英文名称,该链接告诉了常用中文的英文名称:
font-family:中文字体的英文名称:https://www.cnblogs.com/chendc/p/9298832.html
#该block讲述如何在matplotlib里面,修改字体默认属性,完成全局字体的更改。
plt.rcParams['font.sans-serif'] = ['SimSun'] # 指定默认字体为新宋体。
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时 负号'-' 显示为方块和报错的问题。
#局部字体的修改方法1
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.plot(x, label='小示例图标签')
# 直接用字体的名字
plt.xlabel('x 轴名称参数', fontproperties='Microsoft YaHei', fontsize=16) # 设置x轴名称,采用微软雅黑字体
plt.ylabel('y 轴名称参数', fontproperties='Microsoft YaHei', fontsize=14) # 设置Y轴名称
plt.title('坐标系的标题', fontproperties='Microsoft YaHei', fontsize=20) # 设置坐标系标题的字体
plt.legend(loc='lower right', prop={"family": 'Microsoft YaHei'}, fontsize=10) ; # 小示例图的字体设置
2、Tick上的文本
设置**tick(刻度)和ticklabel(刻度标签)**也是可视化中经常需要操作的步骤,matplotlib既提供了自动生成刻度和刻度标签的模式(默认状态),同时也提供了许多让使用者灵活设置的方式。
2.1、简单模式
可以使用axis的set_ticks方法手动设置标签位置,使用axis的set_ticklabels方法手动设置标签格式
x1 = np.linspace(0.0, 5.0, 100)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
# 使用axis的set_ticks方法手动设置标签位置的例子,该案例中由于tick设置过大,所以会影响绘图美观,不建议用此方式进行设置tick
fig, axs = plt.subplots(2, 1, figsize=(5, 3), tight_layout=True)
axs[0].plot(x1, y1)
axs[1].plot(x1, y1)
# axs[1].xaxis.set_ticks(np.arange(0., 10.1, 2.));
axs[1].xaxis.set_ticks(np.arange(0, 10.1, 0.5));
# 使用axis的set_ticklabels方法手动设置标签格式的例子
fig, axs = plt.subplots(2, 1, figsize=(5, 3), tight_layout=True)
axs[0].plot(x1, y1)
axs[1].plot(x1, y1)
ticks = np.arange(0., 8.1, 2.)
tickla = [f'{tick:1.2f}' for tick in ticks] # 格式化数字
axs[1].xaxis.set_ticks(ticks)
axs[1].xaxis.set_ticklabels(tickla)
[Text(0.0, 0, '0.00'),
Text(2.0, 0, '2.00'),
Text(4.0, 0, '4.00'),
Text(6.0, 0, '6.00'),
Text(8.0, 0, '8.00')]
#一般绘图时会自动创建刻度,而如果通过上面的例子使用set_ticks创建刻度可能会导致tick的范围与所绘制图形的范围不一致的问题。
#所以在下面的案例中,axs[1]中set_xtick的设置要与数据范围所对应,然后再通过set_xticklabels设置刻度所对应的标签
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 1, figsize=(6, 4), tight_layout=True)
x1 = np.linspace(0.0, 6.0, 100)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
axs[0].plot(x1, y1)
axs[0].set_xticks([0,1,2,3,4,5,6])
axs[1].plot(x1, y1)
axs[1].set_xticks([0,1,2,3,4,5,6])#要将x轴的刻度放在数据范围中的哪些位置
axs[1].set_xticklabels(['zero','one', 'two', 'three', 'four', 'five','six'],#设置刻度对应的标签
rotation=30, fontsize='large')#rotation选项设定x刻度标签倾斜30度。 "large",'small'
axs[1].xaxis.set_ticks_position('top')#set_ticks_position()方法是用来设置刻度所在的位置,常用的参数有bottom、top、both、none
print(axs[1].xaxis.get_ticklines());
<a list of 14 Line2D ticklines objects>
2.2、Tick Locators and Formatters
除了上述的简单模式,还可以使用Tick Locators and Formatters完成对于刻度位置和刻度标签的设置。
其中Axis.set_major_locator:https://matplotlib.org/stable/api/_as_gen/matplotlib.axis.Axis.set_major_locator.html
和Axis.set_minor_locator:https://matplotlib.org/stable/api/_as_gen/matplotlib.axis.Axis.set_minor_locator.html
方法用来设置标签的位置。
Axis.set_major_formatter:https://matplotlib.org/stable/api/_as_gen/matplotlib.axis.Axis.set_major_formatter.html
和Axis.set_minor_formatter:https://matplotlib.org/stable/api/_as_gen/matplotlib.axis.Axis.set_minor_formatter.html
方法用来设置标签的格式。
这种方式的好处是不用显式地列举出刻度值列表。
set_major_formatter和set_minor_formatter这两个formatter格式命令可以接收字符串格式(matplotlib.ticker.StrMethodFormatter)或函数参数(matplotlib.ticker.FuncFormatter)来设置刻度值的格式 。
2.2.1、Tick Formatters
修改刻度样式
# 接收字符串格式的例子
fig, axs = plt.subplots(2, 2, figsize=(8, 5), tight_layout=True)
for n, ax in enumerate(axs.flat):
ax.plot(x1*10., y1)
formatter = matplotlib.ticker.FormatStrFormatter('%1.1f')
axs[0, 1].xaxis.set_major_formatter(formatter)
formatter = matplotlib.ticker.FormatStrFormatter('-%1.1f')
axs[1, 0].xaxis.set_major_formatter(formatter)
formatter = matplotlib.ticker.FormatStrFormatter('%1.3f')
axs[1, 1].xaxis.set_major_formatter(formatter);
# 接收函数的例子
def formatoddticks(x, pos):
"""Format odd tick positions."""
if x % 2:
return f'{x:1.2f}'
else:
return ''
fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
ax.plot(x1, y1)
ax.xaxis.set_major_formatter(formatoddticks); # 主刻度设置
# ax.xaxis.set_minor_formatter(formatoddticks); # 副刻度设置
2.2.2、Tick Locators
修改刻度位置
在普通的绘图中,我们可以直接通过上图的set_ticks进行设置刻度的位置,缺点是需要自己指定或者接受matplotlib默认给定的刻度。
当需要更改刻度的位置时,matplotlib给了常用的几种locator的类型。如果要绘制更复杂的图,可以先设置locator的类型,然后通过axs.xaxis.set_major_locator(locator)绘制即可
locator=plt.MaxNLocator(nbins=7)#自动选择合适的位置,并且刻度之间最多不超过7(nbins)个间隔 locator=plt.FixedLocator(locs=[0,0.5,1.5,2.5,3.5,4.5,5.5,6])#直接指定刻度所在的位置
locator=plt.AutoLocator()#自动分配刻度值的位置
locator=plt.IndexLocator(offset=0.5, base=1)#面元间距是1,从0.5开始
locator=plt.MultipleLocator(1.5)#将刻度的标签设置为1.5的倍数
locator=plt.LinearLocator(numticks=5)#线性划分5等分,4个刻度
# 接收各种locator的例子
fig, axs = plt.subplots(2, 2, figsize=(8, 5), tight_layout=True)
for n, ax in enumerate(axs.flat):
ax.plot(x1*10., y1)
locator = matplotlib.ticker.AutoLocator() #自动分配刻度值的位置
axs[0, 0].xaxis.set_major_locator(locator)
locator = matplotlib.ticker.MaxNLocator(nbins=3) #自动选择合适的位置,并且刻度之间最多不超过3(nbins)个间隔
axs[0, 1].xaxis.set_major_locator(locator)
locator = matplotlib.ticker.MultipleLocator(5) #将刻度的标签设置为5的倍数
axs[1, 0].xaxis.set_major_locator(locator)
locator = matplotlib.ticker.FixedLocator([0,7,14,21,28]) #直接指定刻度所在的位置
axs[1, 1].xaxis.set_major_locator(locator);
# 此外matplotlib.dates 模块还提供了特殊的设置日期型刻度格式和位置的方式
# 特殊的日期型locator和formatter
locator = mdates.DayLocator(bymonthday=[1,15,25])
formatter = mdates.DateFormatter('%b %d')
fig, ax = plt.subplots(figsize=(5, 3), tight_layout=True)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
base = datetime.datetime(2017, 1, 1, 0, 0, 1)
time = [base + datetime.timedelta(days=x) for x in range(len(x1))]
ax.plot(time, y1)
ax.tick_params(axis='x', rotation=70);
# 设置主副刻度样本
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, ScalarFormatter
fig, ax = plt.subplots()
ax.plot([0, 5, 10, 15, 20], [3, 2, 1, 2, 4])
Axis.set_minor_locator(ax.xaxis, MultipleLocator(1))
Axis.set_minor_formatter(ax.xaxis, ScalarFormatter())
# Axis.set_major_formatter(ax.xaxis, ScalarFormatter())
ax.tick_params(axis ='both', which ='major',
labelsize = 14, pad = 12,
colors ='g')
ax.tick_params(axis ='both', which ='minor',
labelsize = 8, colors ='b')
plt.title("Matplotlib.axis.Axis.set_minor_formatter() \n Function Example", fontsize = 14, fontweight ='bold')
plt.show()
参考文章:
设置y轴刻度_Matplotlib最详细的刻度调整:https://blog.youkuaiyun.com/weixin_34498545/article/details/112631706
# 刻度设置
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
plt.figure(figsize = (7,6),dpi = 100) # 新建一个对象
ax = plt.gca() # 获取当前绘图区域
# 调整坐标轴范围
ax.set_xlim(0,5)
ax.set_ylim(0,5)
# 坐标轴添加文字
ax.set_xlabel("我是X轴",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
ax.set_ylabel("我是Y轴",fontsize = 14,color = 'b',alpha = 0.7,bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='blue',lw=1 ,alpha=0.7))
# 修改默认的刻度文字
ax.set_xticklabels(['A','B','C','D','E','F','G'])
ax.set_yticklabels(['鉴','图','化','视','可','注','关'],family = 'SimHei',fontsize = 14)
# 刻度设置
# 主刻度设置
xmajorLocator = MultipleLocator(1)
ax.xaxis.set_major_locator(xmajorLocator)
ymajorLocator = MultipleLocator(1)
ax.yaxis.set_major_locator(ymajorLocator)
# 副刻度设置
xminorLocator = MultipleLocator(0.25)
ax.xaxis.set_minor_locator(xminorLocator)
yminorLocator = MultipleLocator(0.25)
ax.yaxis.set_minor_locator(yminorLocator)
# 副刻度数值标注
xminorFormatter = FormatStrFormatter('%0.2f')
ax.xaxis.set_minor_formatter(xminorFormatter)
# 主刻度线延长
ax.tick_params(which='major',length=8,labelsize=16)
ax.tick_params(which='minor',length=4)
plt.show()
3、legend(图例)
在具体学习图例之前,首先解释几个术语:
legend entry(图例条目)
每个图例由一个或多个legend entries组成。一个entry包含一个key和其对应的label。
legend key(图例键)
每个legend label左面的colored/patterned marker(彩色/图案标记)
legend label(图例标签)
描述由key来表示的handle的文本
legend handle(图例句柄)
用于在图例中生成适当图例条目的原始对象
以下面这个图为例,右侧的方框中的共有两个legend entry;
两个legend key,分别是一个蓝色和一个黄色的legend key;
两个legend label,一个名为‘Line up’和一个名为‘Line Down’的legend label
图例的绘制同样有OO模式和pyplot模式两种方式,写法都是一样的,使用legend()即可调用。
以下面的代码为例,在使用legend方法时,我们可以手动传入两个变量,句柄和标签,用以指定条目中的特定绘图对象和显示的标签值。
当然通常更简单的操作是不传入任何参数,此时matplotlib会自动寻找合适的图例条目。
fig, ax = plt.subplots()
line_up, = ax.plot([1, 2, 3], label='Line 2')
line_down, = ax.plot([3, 2, 1], label='Line 1')
ax.legend(handles = [line_up, line_down], labels = ['Line Up', 'Line Down']);
3.1、设置图例位置
loc参数接收一个字符串或数字表示图例出现的位置
ax.legend(loc=‘upper center’) 等同于ax.legend(loc=9)
位置对照表如下:
fig,axes = plt.subplots(1,4,figsize=(10,4))
for i in range(4):
axes[i].plot([0.5],[0.5])
axes[i].legend(labels='a',loc=i) # 观察loc参数传入不同值时图例的位置
fig.tight_layout()
3.2、设置图例边框及背景
fig = plt.figure(figsize=(10,3))
axes = fig.subplots(1,3)
for i, ax in enumerate(axes):
ax.plot([1,2,3],label=f'ax {i}')
axes[0].legend(frameon=False) #去掉图例边框
axes[1].legend(edgecolor='blue') #设置图例边框颜色
axes[2].legend(facecolor='gray'); #设置图例背景颜色,若无边框,参数无效
3.3、设置图例标题
fig,ax =plt.subplots()
ax.plot([1,2,3],label='label')
ax.legend(title='legend title');
4、思考题
请尝试使用两种方式模仿画出下面的图表(重点是柱状图上的标签),本文学习的text方法和matplotlib自带的柱状图标签方法bar_label。
4.1、text方法绘图
# text方法绘图
# Python数据可视化利器Matplotlib,如何绘制横向柱形图:https://blog.youkuaiyun.com/TH_NUM/article/details/80601800
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体为新宋体。
# 数据准备
name=['Jim','Slim','Harry','Dick','Tom']
performance=[12.5,8.20,10.00,10.20,10.00]
num = [0.72,0.70,0.16,0.25,0.71]
#图像绘制
fig = plt.figure(figsize=(16,10))
ax = fig.add_subplot()
b = ax.barh(name,performance,color='#6699CC')
# 调整坐标轴范围 & 坐标轴添加文字
ax.set_xlim(0,16)
ax.tick_params(axis ='both', which ='major',labelsize = 16, pad = 12)
# 坐标轴标题
ax.set_xlabel("Performance",fontproperties='Microsoft YaHei',fontsize = 20)
plt.title("How fast do you want to go today?", fontsize = 22)
#添加数据标签
i = 0
for rect in b:
w=rect.get_width()
ax.annotate("",
xy=(w-num[i], i), xycoords='data',
xytext=(w+num[i], i), textcoords='data',
arrowprops=dict(arrowstyle="-", connectionstyle="arc3") #rad 修改折线曲率
);
ax.text(w+num[i]+0.1,i-0.05, '±%0.2f'%num[i], fontproperties='Microsoft YaHei',fontsize = 16,color='b')
i += 1
4.2、bar_label方法绘图
# matplotlib自带的柱状图标签方法bar_label
# Python数据可视化利器Matplotlib,如何绘制横向柱形图:https://blog.youkuaiyun.com/TH_NUM/article/details/80601800
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体为新宋体。
# 数据准备
name=['Jim','Slim','Harry','Dick','Tom']
performance=[12.5,8.20,10.00,10.20,10.00]
num = ['——±0.72','——±0.70','—±0.16','—±0.25','——±0.71']
#图像绘制
fig = plt.figure(figsize=(16,10))
ax = fig.add_subplot()
b = ax.barh(name,performance,color='#6699CC')
# 调整坐标轴范围 & 坐标轴添加文字
ax.set_xlim(0,16)
ax.tick_params(axis ='both', which ='major',labelsize = 16, pad = 12)
# 坐标轴标题
ax.set_xlabel("Performance",fontproperties='Microsoft YaHei',fontsize = 20)
plt.title("How fast do you want to go today?", fontsize = 22)
#添加数据标签
plt.bar_label(b,num, label_type='edge',padding=-15,fontsize = 20,color = 'b');
参考链接:
python使用matplotlib给柱状图添加数据标签bar_label():https://www.jb51.net/article/239984.htm
一行代码简单搞定matplotlib柱状图显示数据标签:http://gg.gg/129gnn
Python中如何使用matplotlib给柱状图添加数据标签(bar_label()):
https://blog.youkuaiyun.com/weixin_43981621/article/details/123217346
Python数据可视化利器Matplotlib,如何绘制横向柱形图:
https://blog.youkuaiyun.com/TH_NUM/article/details/80601800
Matplotlib 文本、箭头的使用方法:https://www.cnblogs.com/lavender1221/p/12792153.html