2

Look at this pretty graph.

enter image description here

Is there a way, in matplotlib, to make parts of the red and green graph invisible (where f(x)=0)?

Not just those, but also the single line segment where the flat part connects to the sine curve.

Basically, is it possible to tell matplotlib to only plot graph on a certain interval and not draw the rest (or vice versa)?

2
  • You need to provide us some code and data to reproduce the figure and then provide solution. Otherwise, anyone would have to write the code from scratch for you and that's just too much work. It is possible but one needs some starting data Commented Jan 10, 2019 at 18:21
  • I'll add some code when I get home. Commented Jan 10, 2019 at 18:26

1 Answer 1

2

You could try replacing your points of interest with np.nan as shown below:

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

That's it, I just need to set it to np.nan instead of 0. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.