Skip to content Skip to sidebar Skip to footer

Why Does This Plot Fail When Adding Scatter But It Works When Removing It?

I am using the following to plot a x date and y decimal. It works great without the commented line (scatter), but if you uncomment it it shows all in one x axis what am I missing?

Solution 1:

The problem is the autoscaling of Collections. This is the underlying issue, which is long known, but hard to come by. There are certain cases, where autoscaling does not work reliably, when values are too close to each other. This is unfortunately often the case for dates.

import matplotlib.pyplotas plt
import datetime

fig, ax = plt.subplots()

x1 = [datetime.date(2018,11,20),datetime.date(2018,11,22)]
y1 = [1,2]

plt.scatter(x1,y1, s=50, color="red")
plt.plot(x1,y1, marker="o", ms=3, zorder=3)

plt.show()

enter image description here

You can however create a plot first. This will determine the autoscaling limits sufficiently. After that plot the scatter.

import matplotlib.pyplotas plt
import datetime

fig, ax = plt.subplots()

x1 = [datetime.date(2018,11,20),datetime.date(2018,11,22)]
y1 = [1,2]

plt.plot(x1,y1, marker="o", ms=3)
plt.scatter(x1,y1, s=50, color="red")

plt.show()

enter image description here

Solution 2:

try setting the limits for both x and y axes:

plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.xlim(datetime.date(2018,11,20),datetime.date(2018,11,22))
plt.ylim(0.1,0.4)
plt.show()

Post a Comment for "Why Does This Plot Fail When Adding Scatter But It Works When Removing It?"