Matplotlib可视化中英文显示无bug切换(助力SCI高水平论文)

一、纯英文图像可视化

  • 直接Times new roman格式,在代码最前面全局设置即可,需要给出字体的路径
font_path = '/home/jie/Downloads/times.ttf'
prop = font_manager.FontProperties(fname=font_path)
plt.rcParams['font.family'] = prop.get_name()

labels = ['C1', 'C2', 'C3', 'C4', 'C5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35       # the width of the bars: can also be len(x) sequence
# ========= exhibit the bar image ===========

x = np.arange(len(labels))  # the label locations

fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(1, 1, 1)

plt.bar(x - width/2, men_means, width, label='men')
plt.bar(x + width/2, women_means, width, label='women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xlabel('Class')
ax.set_ylabel('Number')
# ax.set_title('Three types rock classification result')
ax.set(xticks=x, xticklabels=labels)
ax.set_xlabel(xlabel='men', fontsize=15)
ax.set_ylabel(ylabel='Number', fontsize=15)
ax.legend(loc="upper right", fontsize=15)
plt.yticks(fontsize=15)
plt.xticks(fontsize=15)

# 添加数据标签
for a, b in zip(x-width/2, men_means):
    plt.text(a, b, '%d' % (b) , ha='center', va='bottom', fontsize=10)
for a, b in zip(x + width/2, women_means):
    plt.text(a, b, '%d' % (b) , ha='center', va='bottom', fontsize=10)
fig.tight_layout()
plt.show()

在这里插入图片描述

二、中英文互搭模式

  • 有时候需要显示中文的图名、坐标轴名,然后对刻度值或者其他的数字依旧需要Times new roman格式。切忌在中文之前一定记得加u,然后一句话中同时存在中文和英文则需要有转义符ax.set_title("考试成绩\n$\mathrm{Scores\ higher\ than\ 90}$"),详情见下图图名。
config = {
    "font.family": "serif",  # 使用衬线体
    "font.serif": ["SimHei"],  # 全局默认使用衬线宋体
    "font.size": 14,  # 五号,10.5磅
    "axes.unicode_minus": False,
    "mathtext.fontset": "stix",  # 设置 LaTeX 字体,stix 近似于 Times 字体
}
plt.rcParams.update(config)

labels = ['C1', 'C2', 'C3', 'C4', 'C5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35       # the width of the bars: can also be len(x) sequence
# ========= exhibit the bar image ===========

x = np.arange(len(labels))  # the label locations

fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(1, 1, 1)

plt.bar(x - width/2, men_means, width, label=u'男生')
plt.bar(x + width/2, women_means, width, label=u'女生')
# #设置图例并且设置图例的字体及大小
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 15,
}
# plt.legend(prop=font1)
# 坐标系标签使用西文字体
ticklabels_style = {
    "fontname": "Times New Roman",
    "fontsize": 12,  # 小五号,9磅
}
#设置坐标刻度值的大小以及刻度值的字体
plt.yticks(**ticklabels_style)
plt.xticks(**ticklabels_style)

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xlabel(u'班级')
ax.set_ylabel(u'数量/(人)')
# ax.set_title('Three types rock classification result')
ax.set(xticks=x, xticklabels=labels)
ax.set_xlabel(xlabel=u'班级', fontsize=15)
ax.set_ylabel(ylabel=u'数量/(人)', fontsize=15)
ax.legend(loc="upper right", fontsize=15)
ax.set_title("考试成绩\n$\mathrm{Scores\ higher\ than\ 90}$")

# 添加数据标签
for a, b in zip(x-width/2, men_means):
    plt.text(a, b, '%d' % (b) , ha='center', va='bottom', fontsize=10, fontproperties = font1)
for a, b in zip(x + width/2, women_means):
    plt.text(a, b, '%d' % (b) , ha='center', va='bottom', fontsize=10, fontproperties = font1)
fig.tight_layout()
# plt.show()
plt.savefig(os.path.join('./柱状图.jpg'), dpi=1280, bbox_inches='tight')
plt.close()

在这里插入图片描述
另:本团队由数名top高校博硕士组成,目前正在做的项目有基于深度学习技术、三维激光点云、SLAM等在安防、自动驾驶领域的感知算法研究和落地以及工业生产智能化改造。
有项目意向的可交流沟通
QQ:812108841

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值