【Matplotlib】解决中文乱码、设置图例位置、使用子图

解决中文乱码
import matplotlib.pyplot as plt

x = ['北京', '上海', '深圳', '广州']
y = [60000, 58000, 50000, 52000]
plt.rcParams['font.sans-serif']=['SimHei']  #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False    #用来正常显示负号

plt.plot(x, y)
plt.show()
设置图例位置
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 50)
print(x)
y = np.sin(x)
print(y)

plt.title("sin(x) & 2sin(x)")
plt.plot(x, y, label="sin(x)")
plt.plot(x, y * 2, label="2sin(x)")
# plt.legend()
plt.legend(loc='upper left')    #设置图例位置
plt.show()
使用子图
案例一:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 50)
ax1 = plt.subplot(2, 2, 1)  # (行,列,活跃区)

plt.title('111111111')
plt.plot(x, np.sin(x), 'r', label="sin(x)")
plt.legend(loc='best')

ax2 = plt.subplot(2, 2, 2, sharey=ax1)  # 与 ax1 共享y轴
plt.plot(x, 2 * np.sin(x), 'g', label="sin(x)")
plt.legend(loc='best')
ax3 = plt.subplot(2, 2, 3)
plt.plot(x, np.cos(x), 'b', label="sin(x)")
plt.legend(loc='best')
ax4 = plt.subplot(2, 2, 4, sharey=ax3)  # 与 ax3 共享y轴
plt.plot(x, 2 * np.cos(x), 'y', label="sin(x)")

plt.legend(loc='best')
plt.show()

在这里插入图片描述

案例二:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 50)
print(x)
print(type(x))

ax1 = plt.subplot(2, 1, 1)  # (行,列,活跃区)
plt.plot(x, np.sin(x), 'o')

ax2 = plt.subplot(2, 3, 4)
plt.plot(x, 2 * np.sin(x), 'g')

ax3 = plt.subplot(2, 3, 5, sharey=ax2)
plt.plot(x, np.cos(x), 'b')

ax4 = plt.subplot(2, 3, 6, sharey=ax2)
plt.plot(x, 2 * np.cos(x), 'y')

plt.show()

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用matplotlib绘制图形时,如果需要显示中文字符,可能会遇到中乱码的问题。解决中文乱码问题的方法如下: 1. 设置字体:可以通过设置字体来解决中文乱码问题。首先,需要下载并安装支持中文的字体,如SimHei、Microsoft YaHei等。然后,在代码中使用`matplotlib.rcParams`来设置全局字体,例如: ```python import matplotlib.pyplot as plt import matplotlib # 设置字体为SimHei matplotlib.rcParams['font.family'] = 'SimHei' # 绘制图形 plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('横轴') plt.ylabel('纵轴') plt.title('中文标题') plt.show() ``` 2. 使用Unicode编码:另一种解决中文乱码问题的方法是使用Unicode编码来表示中文字符。在代码中,可以使用Unicode字符串来显示中文字符,例如: ```python import matplotlib.pyplot as plt # 绘制图形 plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel(u'横轴') plt.ylabel(u'纵轴') plt.title(u'中文标题') plt.show() ``` 3. 使用中文字体文件:如果前两种方法无法解决中文乱码问题,可以尝试使用中文字体文件。首先,需要下载并安装中文字体文件,如SimHei.ttf、Microsoft YaHei.ttf等。然后,在代码中使用`matplotlib.font_manager.FontProperties`来设置字体属性,例如: ```python import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 设置字体属性 font = FontProperties(fname='SimHei.ttf') # 绘制图形 plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('横轴', fontproperties=font) plt.ylabel('纵轴', fontproperties=font) plt.title('中文标题', fontproperties=font) plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Koma_zhe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值