省略matplotlib图中的连接线,例如y = tan(x) [英] Omit joining lines in matplotlib plot e.g. y = tan(x)
本文介绍了省略matplotlib图中的连接线,例如y = tan(x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图y = tan(x)
,我想删除垂直线(见下文).
I have the graph y = tan(x)
and I want to remove the vertical lines (see below).
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
# Choose evenly spaced x intervals
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
# plot y = tan(x)
plt.plot(x, np.tan(x))
# Set the range of the axes
plt.axis([-2*np.pi, 2*np.pi, -2, 2])
# Include a title
plt.title('y = tan(x)')
# Optional grid-lines
plt.grid()
# Show the graph
plt.show()
以下是图形(包括不需要的垂直线):
Here is the graph (including unwanted vertical lines):
是否可以在不对x间隔设置适当间隙的情况下删除垂直线?
Can I remove the vertical lines without setting appropriate gaps into the x intervals?
推荐答案
You can check the difference between successive data points using diff
and then identify where the difference is negative and replace these values with NaN
to create a visual break in the plotted line
# Compute the tangent for each point
y = np.tan(x)
# Insert a NaN where the difference between successive points is negative
y[:-1][np.diff(y) < 0] = np.nan
# Plot the resulting discontinuous line
plt.plot(x, y)
这篇关于省略matplotlib图中的连接线,例如y = tan(x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文