Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib y axis values are not ordered [duplicate]

I am trying to plot using matplotlib. The plot showed a problem that the Y axis is not ordered.

Here is the code.

# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
I020 = [ line.strip('\n').split(",") for line in 
open(r'D:\Users\a0476\Anaconda3\TickData\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 = [ line[1] for line in I020 ]
order = np.argsort(Time1)
xs = np.array(Time1)[order]
ys = np.array(Solar)[order]
plt.title('Solar data')
plt.xlabel('Time')
plt.ylabel('Solar')
ax.plot_date(xs, ys, 'k-')
hfmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(hfmt)
plt.show()

The CSV data

time        solar
7000000     50.35
8000000     41.01
9000000     69.16
10000000    94.5
11000000    111.9
12000000    103
13000000    98.6
14000000    36.45
15000000    34.74
16000000    34.17
17000000    34.6

enter image description here

like image 877
林意專 Avatar asked Jan 02 '18 14:01

林意專


People also ask

What does Xticks do in matplotlib?

xticks() Function. The annotate() function in pyplot module of matplotlib library is used to get and set the current tick locations and labels of the x-axis.

How do I get rid of Xticks in matplotlib?

Matplotlib removes both labels and ticks by using xticks([]) and yticks([]) By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis.

What is Antialiased in matplotlib?

The antialiased keyword argument controls whether or not a particular matplotlib artist (e.g. line, polygon, etc) is drawn with antialising or not. Non-antialiased plotting will be faster, so if you're plotting a large amount of data, it can be worthwhile to turn it off.


1 Answers

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

like image 74
DavidG Avatar answered Oct 09 '22 13:10

DavidG