matplotlib - 如何绘制部分彩色的线?
解决方案
这里最简单的解决方案可能是在np.wherex_lim
找到的相应索引处对数据进行切片:
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
y = np.cos(x)*np.exp(-x/2)
# specify your x limitation
x_lim = np.pi
# find the first corresponding idx where the condition x>=x_lim hold
x_lim_idx = np.where(x>=x_lim)[0][0]
# plot sliced data
plt.plot(x[:x_lim_idx],y[:x_lim_idx],'r')
plt.plot(x[x_lim_idx:],y[x_lim_idx:],'b')
这给出了x_lim = np.pi
:
如果线条之间的剩余间隙困扰您,x
例如对于小的离散化,您仍然可以通过使两个切片重叠来关闭它。
推荐阅读
- python - Python 记录通过命令提示符的所有内容
- uber-api - 优步抛出随服务器令牌提供的无效 OAuth 2.0 凭据
- mysql - 使用 Select 查询在 mysql 中创建更新脚本,语法问题?
- python - 使用 Apache Airflow 使用 PySpark 代码执行 Databricks Notebook
- python - 适用于 python2.7 谷歌应用引擎的 Cloud Tasks API
- charts - Highcharts:如何仅显示最后一个系列的值
- python - 在 tkinter 中永久更改标签值
- c++ - 常量正确性、标准移动和智能指针
- php - 如何在视图中显示存储中的pdf文件
- json - 引用 JSON 对象