Python常用小Tips:(1) matplotlib作图

1. matplotlib 图例正常显示中文和正负号

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

2. 给同一幅图中的不同线加标签

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
data = np.random.random(10)
label = "随机曲线"
plt.plot(data,label=label)  //给图例加标签
plt.legend()	//显示图例
plt.show()

3. 限定曲线标签的位置

在这里插入图片描述

plt.legend(loc="upper left")

可选择的位置有:

表示位置的字符串意义
upper left左上
upper right右上
lower left左下
lower right右下
center left中间偏左
center right中间偏右
lower center中间偏下
upper center中间偏上

当然也可以用下面的数字来代表位置:
在这里插入图片描述
更多有关 legend 的操作,请查阅 matplotlib图例legend语法及设置的方法

4. 设置 x轴,y轴的数值范围

在这里插入图片描述

data = np.random.random(100)

plt.plot(data)
plt.xlim(0,100)
plt.ylim(0,1)
plt.show()

5. 设置刻度的显示范围、字体大小、倾斜角度

在这里插入图片描述

data = np.random.random(100)

plt.plot(data)
plt.xlim(0,100)
plt.ylim(0,1)
plt.yticks(ticks=np.arange(0,1,0.1),fontsize=7,rotation=45)
plt.xticks(ticks=np.arange(0,100,5),fontsize=7,rotation=45)
plt.show()

6. 刻度个性化设置

在这里插入图片描述

  • 刻度标签
  • 刻度标签的字体: times new roman
plt.yticks(ticks=[1,2,3,4],labels=['dog','cat','horse','car'],fontsize=7,rotation=45,fontproperties='Times New Roman')
plt.xticks(ticks=[1,2,3,4],labels=['dog','cat','horse','car'],rotation=45,fontproperties='Times New Roman')
plt.show()

7. 给 x 轴 和 y轴加轴标签(区别于刻度标签,不要弄混)

在这里插入图片描述

plt.xlabel("我是大傻子")
plt.ylabel("你是小傻子")

8. 保存图片的像素精度

plt.savefig('./test.png',dpi=200)

9. subplot 的使用方法

参阅网址:https://www.cnblogs.com/chenqionghe/p/12355018.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

暖仔会飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值