Matplotlib y axis values are not ordered [duplicate]

Solution 1:

The reason this happens is because your data is being plotted as strings.

The solution is to convert your y axis data to floats. This can be done by simply casting to a float in your list comprehension:

Solar = [float(line[1]) for line in I020]

I would also suggest to use matplotlib's auto formatting of the x -axis when using dates/times. This will rotate the labels etc to make the graph look better:

plt.gcf().autofmt_xdate()

Your example becomes:

I020 = [ line.strip('\n').split(",") for line in open('PV5sdata1.csv')][1:]
Time = [datetime.datetime.strptime(line[0],"%H%M%S%f") for line in I020]
Time1 = [mdates.date2num(line) for line in Time]
Solar = [float(line[1]) for line in I020]

xs = np.array(Time1)  # You don't really need to do this but I've left it in
ys = np.array(Solar)

fig, ax = plt.subplots() # using matplotlib's Object Oriented API

ax.set_title('Solar data')
ax.set_xlabel('Time')
ax.set_ylabel('Solar')
ax.plot_date(xs, ys, 'k-')

hfmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(hfmt)
plt.gcf().autofmt_xdate()

plt.show()

Which gives:

enter image description here