Gilles L
Gilles L

Reputation: 175

Data not appearing on a python plot

I have a dataframe with date as index, floats as columns, filled with mostly NaN and a few floats.

I am plotting this dataframe using :

fig, ax = plt.subplots()
plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx', title='xx')
ax.grid()
ax.legend()

The plot window open but with no data appearing. But if I use markers instead of line, the data point will appears.

What should I correct to plot my graphs as lines?

edit Thanks, it worked like this :

s1 = np.isfinite(df2[11][2:])
fig, ax = plt.subplots()
plot(df2.index[2:][s1],df2[11][2:].values[s1], linestyle='-',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx',title='xx')
ax.grid()
ax.legend()

Upvotes: 1

Views: 1249

Answers (2)

Richard Rublev
Richard Rublev

Reputation: 8164

Try

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
plt.set(xlabel='xx', ylabel='xx', title='xx')
plt.grid()
plt.legend()
plt.show()

Upvotes: 1

Piotrek
Piotrek

Reputation: 1530

In your case matplotlib won't draw a line between points separated by NaNs. You can mask NaNs or get rid of them. Have a look at the link below, there are some solutions to draw lines skipping NaNs.

matplotlib: drawing lines between points ignoring missing data

Upvotes: 0

Related Questions