Python直角坐标系画图
Posted yangbocsu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python直角坐标系画图相关的知识,希望对你有一定的参考价值。
Python直角坐标系画图
一、平面直角坐标系
1.1 画一个点
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备 一个点的坐标
X = np.array([1])
Y = np.array([1])
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = 0, xmax = 2) # x轴的范围[0,2]
plt.ylim(ymin = 0, ymax = 2) # y轴的范围[0,2]
plt.xlabel('X')
plt.ylabel('Y')
# 调整x轴刻度(从0到+2,4等分)
plt.xticks(np.linspace(0, 2, 5))
# 调整y轴刻度
plt.yticks(np.linspace(0, 2, 5))
# 获取当前坐标轴gca即get current axis
ax = plt.gca()
ax.spines['top'].set_color('none') # 去掉上、右二侧的边框线
ax.spines['right'].set_color('none')
# # 给坐标轴加箭头
# plt.arrow(0, 2, 0, 0.01, width=0.02, color="k", clip_on=False, head_width=0.02, head_length=0.02)
# plt.arrow(2, 0, 0.01, 0, width=0.02, color="k", clip_on=False, head_width=0.02, head_length=0.02)
plt.title('画一个点') #图的标题
# 2 画图显示 + 保存图像
plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A')
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.2 画两个点
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备 两个点的坐标
X = np.array([1,3])
Y = np.array([1,8])
xmx = X.max() + 1
ymx = Y.max() + 1
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = 0, xmax = xmx) # x轴的范围[0,2]
plt.ylim(ymin = 0, ymax = ymx) # y轴的范围[0,2]
plt.xlabel('X')
plt.ylabel('Y')
# 调整x轴刻度(从0到+2,4等分)
plt.xticks(np.linspace(0, xmx, int(xmx//0.50) + 1))
# 调整y轴刻度
plt.yticks(np.linspace(0, ymx, int(ymx//0.50) + 1))
# 获取当前坐标轴gca即get current axis
ax = plt.gca()
ax.spines['top'].set_color('none') # 去掉上、右二侧的边框线
ax.spines['right'].set_color('none')
# # 给坐标轴加箭头
# plt.arrow(0, 2, 0, 0.01, width=0.02, color="k", clip_on=False, head_width=0.02, head_length=0.02)
# plt.arrow(2, 0, 0.01, 0, width=0.02, color="k", clip_on=False, head_width=0.02, head_length=0.02)
plt.title('画两个点') #图的标题
# 2 画图显示 + 保存图像
plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A')
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.3 画散点图
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备 画一条直线:两点之间可以确定一条直线,①要么给出两点;②要么给出线上的所需要的点
X = np.arange(1,10,1)
Y = np.array([1,3,3,3,5,6,7,8,10])
xmx = X.max() + 2
ymx = Y.max() + 2
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = 0, xmax = xmx) # x轴的范围[0,2]
plt.ylim(ymin = 0, ymax = ymx) # y轴的范围[0,2]
plt.xlabel('X')
plt.ylabel('Y')
plt.title('画散点图') #图的标题
# 2 画图显示 + 保存图像
plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A')
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.4 画直线
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备 画一条直线:两点之间可以确定一条直线,①要么给出两点;②要么给出线上的所需要的点
X = np.arange(1,10,1)
Y = 0.5*X +1 # Y = kx + b
xmx = X.max() + 2
ymx = Y.max() + 2
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = 0, xmax = xmx) # x轴的范围[0,2]
plt.ylim(ymin = 0, ymax = ymx) # y轴的范围[0,2]
plt.xlabel('X')
plt.ylabel('Y')
plt.title('画散点图') #图的标题
# 2 画图显示 + 保存图像
plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A') # 画散点图
plt.plot(X,Y) #画直线
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.5 画抛物线
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备
X = np.arange(-5,5,0.1)
Y = X**2 + 1 # Y = x^2 + b
xmx = X.max() + 2
ymx = Y.max() + 2
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = -xmx, xmax = xmx) # x轴的范围[-xmx,xmx]
plt.ylim(ymin = -1, ymax = ymx) # y轴的范围[-1,xmx]
plt.xlabel('X')
plt.ylabel('Y')
plt.title('画抛物线') #图的标题
# 2 画图显示 + 保存图像
plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A') # 画散点图
plt.plot(X,Y) #画直线
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.6 画反比例函数
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备
X = np.arange(-5,5,0.1)
Y = 1/X # Y = 1/X
xmx = X.max() + 2
ymx = Y.max() + 2
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = -xmx, xmax = xmx) # x轴的范围[-xmx,xmx]
plt.ylim(ymin = -xmx, ymax = ymx) # y轴的范围[-1,xmx]
plt.xlabel('X')
plt.ylabel('Y')
plt.title('画反比例函数') #图的标题
# 2 画图显示 + 保存图像
# plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A') # 画散点图
plt.plot(X,Y) #画直线
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
1.7 画正弦函数
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: dot.py.py
@time: 2022.03.05
#------------------------------------
'''
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 0 数据准备
X = np.arange(-10,10,0.1)
Y = np.sin(X)
xmx = 12
ymx = 2
# 1 设置x,y坐标轴的刻度显示范围
fig = plt.figure()
plt.xlim(xmin = -xmx, xmax = xmx) # x轴的范围[-xmx,xmx]
plt.ylim(ymin = -2, ymax = ymx) # y轴的范围[-1,xmx]
plt.xlabel('X')
plt.ylabel('Y')
plt.title('画正弦函数') #图的标题
# 2 画图显示 + 保存图像
# plt.scatter(X, Y, marker = 'o', alpha=0.4, color="red", label='类别A') # 画散点图
plt.plot(X,Y) #画直线
plt.legend() #label='类别A' 图中显示
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
二、空间直角坐标系
2.1 空间画点
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: zone.py
@time: 2022.03.05
#------------------------------------
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 绘图设置
fig = plt.figure()
ax = Axes3D(fig)
X = np.array([1])
Y = np.array([1])
Z = np.array([1])
ax.scatter(X, Y, Z, c = 'b', marker='o',label="A")
ax.set_xlabel('X 轴')
ax.set_ylabel('Y 轴')
ax.set_zlabel('Z 轴')
plt.legend()
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
2.2 空间画多点
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: zone.py
@time: 2022.03.05
#------------------------------------
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 绘图设置
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(0,6,1)
Y = np.arange(0,6,1)
Z = np.arange(0,6,1)
ax.scatter(X, Y, Z, c = 'b', marker='o',label="A")
ax.set_xlabel('X 轴')
ax.set_ylabel('Y 轴')
ax.set_zlabel('Z 轴')
plt.legend() #label="A"
path = os.getcwd() # 获取当前的工作路径
fileName = "979424151"
filePath = path + "\\\\" + fileName + ".png"
plt.savefig(filePath, dpi=600) # dpi越大,图像越清晰,当然图像所占的存储也大
plt.show()
2.3 空间画直线
# -*-coding:utf-8 -*-
'''
#------------------------------------
@author:By yangbocsu
@file: zone.py
@time: 2022.03.05
#------------------------------------
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import os
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
pltMATLAB简易画图2—普通直角坐标系