John Smith
John Smith

Reputation: 1698

Strange behavior of matplotlib when overlaping two plots

First I have these data and the plot is OK

dic={'x': {0: '1', 1: '3', 2: '4', 3: '7', 4: '9', 5: '10', 6: '11', 7: '13', 8: '14', 9: '16'},
    'y': {0: '0', 1: '0', 2: '0', 3: '1', 4: '0', 5: '1', 6: '1', 7: '1', 8: '1', 9: '1'}}

df = pd.DataFrame(dic)

plt.scatter(df["x"],df["y"])

enter image description here

Then I have these data and the plot is ok too

y2=np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1])

xsimul=np.linspace(0,16,200).reshape(-1,1)
plt.plot(xsimul,y2)

enter image description here

But when I try to create the two plots together in one figure

plt.plot(xsimul,y2)
plt.scatter(df["x"],df["y"])

I got this plot

enter image description here

What is wrong in my code?

Upvotes: 0

Views: 28

Answers (1)

THeek
THeek

Reputation: 76

The data points in the dictionary are strings. These must be converted to integer before you make the plots. This could be done by making use of astype(). This results in the desired plot.

df["x"] = df["x"].astype(int)
df["y"] = df["y"].astype(int)

In this case the complete DataFrame could be converted to integers.

df = df.astype(int)

Upvotes: 1

Related Questions