python - 使 matplotlib 图部分不可见
问题描述
看看这个漂亮的图表。
在 matplotlib 中有没有办法使红色和绿色图形的一部分不可见(其中 f(x)=0)?
不仅是那些,还有平坦部分连接到正弦曲线的单线段。
基本上,是否可以告诉 matplotlib 仅在某个间隔上绘制图形而不绘制其余部分(反之亦然)?
解决方案
np.nan
您可以尝试使用如下所示替换您的兴趣点:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# here is some example data because none was provided in the question;
# it is a quadratic from x=-5:5
x = np.arange(-5, 6)
s = pd.Series(x**2, index=x)
# replace all y values less than 4 with np.nan and store in a new Series object
s_mod = s.apply(lambda y: np.nan if y < 4 else y)
# plot the modified data with the original data
fig, ax = plt.subplots()
s.plot(marker='o', markersize=16, ax=ax, label='original')
s_mod.plot(marker='s', ax=ax, label='modified')
ax.legend()
fig # displays as follows
推荐阅读
- typescript - 在打字稿回调函数中
- mongodb - MongoDB中NULL的索引
- javascript - 数组对象重复数据删除。跟在后面
- rest - 卡夫卡休息代理是解决方案?
- python - 如何在一行中编写两个要在 if 条件下执行的语句?
- c - 由于 SIGFPE、算术异常,除法运算导致执行错误
- javascript - 带有 datalist 的 Javascript 不会显示所有匹配的选项
- c# - 有什么方法可以从 Visual Studio 2008 连接 oracle db 11g 或更高版本
- python - 在 Python PySide2 中使用睡眠时如何在同一函数中看到多个变化
- python - 阿帕奇气流 | 无法更改 airflow.cfg 中的默认时区