Henning
Henning

Reputation: 105

Matplotlib ylim and xlim not working in combination of scatter plot and line plot or fill_between

I encounter a plotting issue I don't understand. Below code shall draw a straight line, fill the area above the line with a colour and plot several scattered dots in it. That all works but if I combine scatter and any of line or fill_between I cannot set the plot limits. The plot area is much larger than it had to be.

So how do I set the plot limits?

from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0,160,100)
MCSample = np.random.normal(112,10,1000)
YSample = np.random.normal(100,2.41,1000)
y_limit = max(160, np.max(YSample))

fig, ax = plt.subplots(1, 1)
ax.plot(x,x, label="Limit State function")
ax.scatter(MCSample,YSample, marker='.', color='b', alpha=0.5)
ax.fill_between(x,y_limit,x, alpha=0.1, color='r')
ax.set_xlim=(0,160)
ax.set_ylim=(0,y_limit)
plt.show()

I'm using Python 3.5.1 and Matplotlib 1.5.1.

Upvotes: 3

Views: 13369

Answers (1)

DavidG
DavidG

Reputation: 25362

In your code you are setting ax.set_xlim to equal (0,160).

All you have to do to make your code work is to get rid of the equal signs as shown below:

ax.set_xlim(0,160)
ax.set_ylim(0,y_limit)  # no equals sign on these 2 lines

Now you are applying those limits to the graph rather than defining them to equal the limits.

Upvotes: 4

Related Questions