python - 如何为图表上的函数之一设置域?

标签 python numpy matplotlib plot

我有一段代码可以在 Python 上绘制两个函数的图表。我只是想知道是否有一种方法可以让我的 x 函数仅在 0 到 0.8 之间的域上,这样它在与 h 相交后就不会继续下去,但我仍然希望 h 继续下去。我有什么办法可以修改这个吗?谢谢。这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerLine2D

t = np.arange(0.0, 1.0, 0.001)
h = 0.1*np.sin(10*t)
x = 4*t-5*t**2

line1, = plt.plot(t, h, label='h(t)')
line2, = plt.plot(t, x, label='x(t)', linestyle='--')

plt.legend(handler_map={line1: HandlerLine2D(numpoints=4)})

plt.xlabel('time')
plt.ylabel('height')
plt.title('Fig 1.')
plt.grid(False)
plt.savefig("Plot.png")
plt.show()   

它看起来是这样的:

enter image description here

最佳答案

简单地对两个数组进行切片:

line2, = plt.plot(t[t<0.8], x[t<0.8], label='x(t)', linestyle='--')

关于python - 如何为图表上的函数之一设置域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47336183/

相关文章:

python - 为具有计算值 NetworkX 的图形编写标题

python - 无法在 jupyter notebook 中导入模块;错误的系统路径

python - 快速查找数组中重复元素的长度和起始索引的方法

python - 如何在 matplotlib 中为 pyplot.show() 设置超时?

python - 根据另一个数据集中的元素位置过滤 pandas 数据帧的快速方法

python - 在 Python/NumPy 中计算矩阵的 Jordan 范式

python - matplotlib 图例符号和文本未对齐

python - 如何在 django 1.7.7 中使用 {{ request.path }}

Python Flask、SQLAlchemy 关系

python - 使用 python 3 以可靠的方式解析文件