二维平面绘图

高中同学作业绘图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import MultipleLocator
x1 = np.arange(0, 2.3, 0.1)
x2 = np.arange(2.2, 3.2, 0.1)

y1 = 8.7 * x1 - 5 * x1 ** 2 
y2 =  32 * x2 - 5 * x2 ** 2 - 51.2
fig = plt.figure(figsize=(20, 15))
ax = fig.add_subplot(111)
plt.plot(x1, y1,color='blue')
plt.plot(x2, y2,color='blue')
plt.xlabel('x')
plt.ylabel('y')
plt.title("M(x)")
x_major_locator=MultipleLocator(0.1)
y_major_locator=MultipleLocator(0.5)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.xlim(0,3.2)
plt.show()

线性规划作业绘图

image

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-1,5,0.1)
y2 = 1 - x
y3 = 5 - (1.0/2) * x
y4 = -(1.0/3) * x + 4.67

plt.axhline(4, color='blue')
plt.plot(x, y2, color='blue')
plt.plot(x, y3, color='blue')
plt.plot(x, y4, color='orange')
#截取x,y的某一部分
plt.xlim((-1,5))
plt.ylim((-1,5))
#设置x,y的坐标描述标签
plt.xlabel("x1")
plt.ylabel("x2",rotation=0)
#设置x刻度的间隔
plt.xticks(np.arange(-1,7,1))
plt.yticks(np.arange(-1,7,1))

ax = plt.gca()#获取当前坐标的位置
#去掉坐标图的上和右
ax.spines['right'].set_color('None')
ax.spines['top'].set_color('None')
#指定坐标的位置
ax.xaxis.set_ticks_position('bottom') # 设置x轴
ax.yaxis.set_ticks_position('left') # 设置y轴
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.show()
posted @ 2021-12-22 21:37  anyiya  阅读(93)  评论(0编辑  收藏  举报