matplotlib设置中文字体为微软雅黑

matplotlib无法设置任何中文字体怎么办?
如何在linux系统下让matplotlib显示中文?

下载微软雅黑字体,把它放在某个目录下。
链接:
https://pan.baidu.com/s/1SCLYpH_MzY7vn0HA0wxxAw?pwd=ft2j

提取码:ft2j

在代码中加入以下代码

from matplotlib.font_manager import FontProperties  
 
# 加载中文字体
#fname为路径
font = FontProperties(fname="/home/mw/project/MSYH_Light_Regular.ttf", size=14)
# 横坐标使用中文时
plt.xticks(fontproperties=font)

# 图例使用中文时
plt.legend(prop=font)

#添加标题
ax.set_title('世界地图',fontproperties = font)

#显示文字
ax.text(v[0], v[1], country, transform=ccrs.Geodetic(), fontsize=8,fontproperties=font)

例如:
绘制一幅世界地图,并从首都北京到所给的其他几个国家的首都之间连上一根虚线,用粗细粗细表示客流量等级大小,并添加上客流量的图例(lagend)。数据:各国首都的经纬度及客流量等级。

“中国”: [116.20, 39.55],
“文莱”: [115.00, 4.52], —1
“意大利”: [12.29, 41.54], —1
“巴西”: [-47.55, -15.47], —1
“英国”: [-0.05, 51.36], —2
“坦桑尼亚”: [35.45, -6.08], —1
“美国”: [-77.02, 39.91],—3
“泰国”: [100.35, 13.45], —2
“韩国”: [126.58, 37.31],—3
“罗马尼亚”: [26.10, 44.27], —1
“俄罗斯”: [37.35, 55.45], —3

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 3),dpi=550)#创建画布
#定义国家首都经纬度及客流量等级
capitals = {"中国": [116.20, 39.55],
            "文莱": [115.00, 4.52],
            "意大利": [12.29, 41.54],
            "巴西": [-47.55, -15.47],
            "英国": [-0.05, 51.36],
            "坦桑尼亚": [35.45, -6.08],
            "美国": [-77.02, 39.91],
            "泰国": [100.35, 13.45],
            "韩国": [126.58, 37.31],
            "罗马尼亚": [26.10, 44.27],
            "俄罗斯": [37.35, 55.45]}

#定义客流量等级
flows = {"中国-文莱": 1,
         "中国-意大利": 1,
         "中国-巴西": 1,
         "中国-英国": 2,
         "中国-坦桑尼亚": 1,
         "中国-美国": 3,
         "中国-泰国": 2,
         "中国-韩国": 3,
         "中国-罗马尼亚": 1,
         "中国-俄罗斯": 3}

#创建投影
ax = plt.axes(projection=ccrs.PlateCarree())

#添加海岸线、边界线、国家名称等特征

ax=fig.add_subplot(projection=ccrs.PlateCarree())#创建经纬投影子图
                                               #central_longitude参数为投影中心位置
ax.add_feature(cfeature.OCEAN.with_scale(scale))#添加海洋
ax.add_feature(cfeature.LAND,color='green')####添加陆地######
ax.add_feature(cfeature.RIVERS,lw=0.25)#####添加河流######
ax.coastlines(resolution='110m',color='black',lw=0.5)#添加海岸线
ax.set_extent([-180, 180, -90, 90], crs=ccrs.PlateCarree())
ax.set_title('世界地图',fontproperties=font)
#绘制绿色点标出所有国家的位置
for c in capitals:
    ax.scatter(capitals[c][0], capitals[c][1], s=50, color='green', transform=ccrs.PlateCarree())
#绘制虚曲线
for c in capitals:
    if c != "中国":
        ax.plot([capitals["中国"][0], capitals[c][0]], [capitals["中国"][1], capitals[c][1]], 
        linestyle='--', linewidth=flows["中国-"+c],color='r')

#添加客流量图例
for i in range(1, 4):
    ax.plot([], [], linestyle='--', linewidth=i, label='客流量等级 '+str(i),color='r')
    ax.legend(prop=font)

#添加国家名称
for c in capitals:
    ax.text(capitals[c][0], capitals[c][1], c, fontsize=8, transform=ccrs.PlateCarree(),fontproperties=font,color='w')

ax.stock_img() #添加浮雕效果或者背景贴图

plt.show()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铃音.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值