Reputation: 11
I am having difficulty getting y-axis labels to sort on the plot by their value in python. see plot I read on another board that their issue was needing to try to convert string to float instead (xlabel and ylabel values are not sorted in matplotlib scatterplot), but when trying to do so on my code it does not remedy the issue. I am pulling data from .dat files and pulling out the temperature from these files to use as the y-label, pinning it to the data and then plotting it. I would like the y-axis to then organize by the value of those labels. Not sure if I am not converting in the right spot or some other issue. Code is below. Help getting my data to sort by the y-axis label value is much appreciated!!
#Temperature depenedent data organization
import glob
import numpy as np
import matplotlib.pyplot as plt
spacer=0 #keeps track of y shift in plots, is zero for first line after +=
labels=[]
label_position=[]
for filename in glob.glob('*_610_1010_*.dat'): #defines what files it is going to perform loops on
linenumber=0
spacer+=40 #normalizer thing, change this number to increase/decrease y spacing
num_lines=sum(1 for line in open(filename))-7 #says how many lines in text file
data=np.zeros((num_lines,2)) #matrix size of data
for line in open(filename, 'r'): # loop within each data file to go through and sort lines
linenumber+=1 #adds one to variable each time loop runs to count line numbers
if linenumber==3:
actual_T=float(line[22:28]) #pulls out actual temperature value from .dat file line 3
labels.append(float(actual_T) ) #using actual_T value for the legend entry of data set
if linenumber==4:
set_T=line[18:25] #pulls out set temperature value from .dat file line 4
if linenumber>7: #remaining contents of dat file that is just the data
array=(line.split("\t")) #data array for that line, splitting columns by deliminator
if linenumber==8:
normalize=np.float(array[1]) #normalizing y in phase and converting to float
container=np.float(array[1])/normalize+spacer #putting y data in container and taking noramlized values and adding spacer to create y-offset
label_position.append(container) #positioning y-axis label with the offset
data[linenumber-8,0]=array[0] #saves frequency values to data, x values
data[linenumber-8,1]=np.float(array[1])/normalize+spacer #saves in phase y values to data
fig=plt.figure(1)
plt.plot(data[:,0], data[:,1], label=actual_T) #creates plot of all data sets from arrays in the loop
plt.xlim(850,1010) #frequency, x, range of plot
plt.xlabel('Frequency (Hz)', fontsize=20)
plt.ylabel('Temperatue (K)', fontsize=20)
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 10 # Set figure width to 10 and height to 8
fig_size[1] = 28
plt.rcParams["figure.figsize"] = fig_size
plt.yticks(label_position,labels) #adding y ticks as the actual_T tied to each data set
fig.savefig('fig1.jpeg') #saves combines graph to current folder in jpeg
plt.show()
Upvotes: 1
Views: 1671
Reputation: 1420
Sort by the value of y-axis before plotting. Here is an example.
import itertools
import matplotlib.pylab as plt
x = [6, 7, 8, 9, 10]
y = [3, 5, 6, 1, 2]
x1 = [6, 7, 8, 9, 15]
y1 = [13, 15, 16, 11, 20]
lists = sorted(zip(*[x, y]))
new_x, new_y = list(zip(*lists))
lists1 = sorted(zip(*[x1, y1]))
new_x1, new_y1 = list(zip(*lists1))
# import operator
# new_x = map(operator.itemgetter(0), lists) # [1, 2, 3, 5, 6]
# new_y = map(operator.itemgetter(1), lists) # [9, 10, 6, 7, 8]
# Plot
plt.plot(new_x, new_y, 'b' , marker="o", lw=2.9)
plt.plot(new_x1, new_y1, 'r' , marker="*", lw=2.9)
plt.grid()
P.S. : I am not sure how your data would look like as you import the same from *.dat file, which I believe is sensed data. I use random data. But the logic stays the same.
For small data, zip
is enough.
new_x, new_y = zip(*sorted(zip(x, y)))
Upvotes: 1