matplotlib.pyplot 标记出曲线上最大点和最小点的位置

废话不多说,直接上代码。
要求:正确安装了matplotlib库

import matplotlib.pyplot as plt
import numpy as np

def demo_test():
    a=np.array([0.15,0.16,0.14,0.17,0.12,0.16,0.1,0.08,0.05,0.07,0.06]);
    max_indx=np.argmax(a)#max value index
    min_indx=np.argmin(a)#min value index
    plt.plot(a,'r-o')
    plt.plot(max_indx,a[max_indx],'ks')
    show_max='['+str(max_indx)+' '+str(a[max_indx])+']'
    plt.annotate(show_max,xytext=(max_indx,a[max_indx]),xy=(max_indx,a[max_indx]))
    plt.plot(min_indx,a[min_indx],'gs')
    plt.show()



if __name__=="__main__":
    demo_test();

效果图如下:
这里写图片描述

  • 10
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 使用Python绘制3个极大值和4个极小值的曲线,步骤如下:1. 导入必要的模块:import numpy as np,import matplotlib.pyplot as plt2. 定义曲线数据:x = np.linspace(-10, 10, 100),y = x ** 33. 定义极大值:max_point=[(-3, -27), (3, 27), (0, 0)]4. 定义极小值:min_point=[(-2, -8), (2, 8), (-4, -64), (4, 64)]5. 绘制曲线:plt.plot(x, y)6. 在曲线标记极大值:for point in max_point: plt.scatter(point[0], point[1], c='r')7. 在曲线标记极小值:for point in min_point: plt.scatter(point[0], point[1], c='b')8. 显示图像: plt.show() ### 回答2: 要编写代码用Python绘制具有3个极大值和4个极小值的曲线,并标记极值,可以使用matplotlib库来实现。 首先,我们导入matplotlib库和numpy库,并创建一个图形对象和子图对象: ```Python import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() ``` 接下来,我们需要定义一个包含3个极大值和4个极小值的函数。这里我们以一个简单的三次多项式函数为例: ```Python def curve(x): return x**3 - 5*x**2 + 4*x + 6 ``` 然后,我们需要生成曲线上的坐标,可以使用numpy库的linspace函数生成一定范围内的等间距,再通过曲线函数计算y值: ```Python x = np.linspace(-5, 5, 100) y = curve(x) ``` 接着,我们可以使用plot函数绘制曲线: ```Python ax.plot(x, y) ``` 然后,我们需要找到极值的坐标。可以使用numpy库的argrelextrema函数找到数组的极值。我们先找到极大值,再找到极小值,并将其坐标存储在两个数组中: ```Python max_points = argrelextrema(y, np.greater)[0] min_points = argrelextrema(y, np.less)[0] max_x = x[max_points] max_y = y[max_points] min_x = x[min_points] min_y = y[min_points] ``` 最后,我们可以使用scatter函数在图中标记极值: ```Python ax.scatter(max_x, max_y, color='r', label='Maxima') ax.scatter(min_x, min_y, color='g', label='Minima') ``` 最后我们调整图的显示范围,加上图例,以及设置标题等: ```Python ax.set_xlim(-5, 5) ax.set_ylim(-5, 10) ax.legend() plt.title("Curve with Extrema") plt.show() ``` 综合以上步骤,完整的代码如下: ```Python import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() def curve(x): return x**3 - 5*x**2 + 4*x + 6 x = np.linspace(-5, 5, 100) y = curve(x) ax.plot(x, y) max_points = argrelextrema(y, np.greater)[0] min_points = argrelextrema(y, np.less)[0] max_x = x[max_points] max_y = y[max_points] min_x = x[min_points] min_y = y[min_points] ax.scatter(max_x, max_y, color='r', label='Maxima') ax.scatter(min_x, min_y, color='g', label='Minima') ax.set_xlim(-5, 5) ax.set_ylim(-5, 10) ax.legend() plt.title("Curve with Extrema") plt.show() ``` 运行该代码,就可以绘制具有3个极大值和4个极小值的曲线,并将极值标记来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值