Matplotlib Python学习

模板

import matplotlib.pyplot as plt
import numpy as np


figure() #生成一个图表
#内容
#操作
plt.show()

入门

x = np.linspace(-1, 1, 50)  # 利用np生成-1到1的50个点
y1 = x ** 2
# 第一幅图
plt.figure()
plt.plot(x, y1)  # 展示出来

在这里插入图片描述

x = np.linspace(-1, 1, 50)  # 利用np生成-1到1的50个点
y1 = x ** 2
y2 = 2 * x + 1
# 第一幅图
plt.figure()
plt.plot(x, y1)  # 展示出来
# 第二幅图
plt.figure(num=2, figsize=(8, 6))  # 画布的长是8,宽是6
plt.plot(x, y2)

在这里插入图片描述

plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')  # 在x = np.linspace(-1, 1, 50),y1 = x ** 2的这幅图中加入线宽1cm,打印出来是--

在这里插入图片描述

plt.xlim(-1, 2)  # 坐标轴x的范围
plt.ylim(-2, 3)

plt.xlabel("I am X.")  # 描述x轴是什么
plt.ylabel("I am Y.")

在这里插入图片描述

new_ticks = np.linspace(-1, 2, 5)  # 生成-1到2的5个点
plt.xticks(new_ticks)  # 将X轴的坐标刻度值修改为-1到2的5个点

在这里插入图片描述

# $ $ 数学形式的字体; 空格无法识别 用\+空格 即可,r表示正则表达式
plt.yticks([-2, -1.8, -1, 1.22, 3],
           [r"$really\ bad$", r"$bad\ \alpha$", r"$normal$", r"$good$", r"$really\ good$"])  # 正则表达式,输出的英文更美观

在这里插入图片描述

# 移动坐标轴
'''
axis:英[ˈæksɪs]n.轴(旋转物体假想的中心线); (尤指图表中的)坐标轴
axis 的复数;axes
'''
# 1.gca='get current axis"获取现在的坐标轴
ax = plt.gca()
# 2.spines轴的脊梁,就是四个边框
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")

ax.xaxis.set_ticks_position("bottom")  # 用bottom代替X轴的刻度
ax.yaxis.set_ticks_position("left")

# 3.挪动x、y的位置
ax.spines['bottom'].set_position(("data", 0))  # x的坐标轴跑到y轴data=-1的位置处,去上面找yticks
ax.spines['left'].set_position(('data', 0))  # data可以换作axes,定位到百分之多少的位置

在这里插入图片描述

'''
legend图例:plt.legend()
'''
line1, = plt.plot(x, y2, label='up')
line2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='down')
plt.legend(handles=[line1, line2], labels=['aa', 'bb'], loc='upper right')  # 线想要传入到handles中需要在赋值时加逗号,给先不同名字,把图例放在右上
plt.legend(handles=[line1, ], labels=['aa', ], loc='best')  # 只想打印line1

在这里插入图片描述

'''
在图片中添加注解Annotation
'''
x = np.linspace(-3, 3, 50)
y = 2 * x + 1
plt.figure(num=3, figsize=(8, 5))
plt.plot(x, y) 

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')  # x轴用bottom代替
ax.spines['bottom'].set_position(('data', 0))  # bottom的值又根据x的data来显示,data是x=[-3,3]的50个点,
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
x0 = 1
y0 = 2 * x0 + 1
plt.scatter(x0, y0)  # 只展示一个点

在这里插入图片描述

plt.scatter(x0, y0, s=50, color='b')  # 只展示一个点,s是大小,'b'=blue
plt.plot([x0, x0], [y0, 0], 'k--', lw=2.5)  # 画一条虚线,两点确定一条直线,(x0,x0)和(y0,0).. k代表黑色,打印出来的是--,线宽2.5

在这里插入图片描述

'''
添加注释method1
'''
plt.annotate(r'$2x+1=%s$' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
             fontsize=16, arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=.2'))  # 添加注释
# xy要基于data对应的点, text的位置基于点的位置横坐标 + 30, 纵坐标 - 30
# arrowprops方向线,connectionstyle线的弧度或角度

在这里插入图片描述

'''
method2
'''
plt.text(-3.7, 3, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$',
         fontdict={'size': 16, 'color': 'r'})  # 开始的位置,内容

在这里插入图片描述

散点图

import matplotlib.pyplot as plt
import numpy as np

n = 1024

X = np.random.normal(0, 1, n)  # 平均数0,方差1,n个数
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)  # 让颜色好看
plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)

plt.show()

在这里插入图片描述

plt.xticks(())
plt.yticks(())

在这里插入图片描述

n = 1024

X = np.random.normal(0, 1, n)  # 平均数0,方差1,n个数
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X)  # 让颜色好看
# plt.scatter(X, Y, s=75, c=T, alpha=0.5)
plt.scatter(np.arange(5), np.arange(5))  # 0,1,2,3,4,5
# plt.xlim(-1.5, 1.5)
# plt.ylim(-1.5, 1.5)
plt.xticks(())
plt.yticks(())

plt.show()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值