Python Forum

Full Version: Dropping all rows of multiple columns after the max of one cell
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay so to be frank I am new to python, and I am trying to interpret some data, this is what I have (below)
so What I am trying to do is make it so that the minimum value for my first column will cause the matplotlib to not plot any values after that specific row (for all three columns, not just area)
Does that make sense?
I thought the pandas pd stuff would do the trick but nope :c Doh Think


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv("C:\\Users\\Owner\\Desktop\\Thunberg,Dametre\\5-29 Data and movies\\New folder (2)\\Data 2.csv", sep=',')
rowmin = df.area.idxmin()
df[:(1 + rowmin)]
fig, ax1 = plt.subplots()
area, pressure, pixel = np.loadtxt ("C:\\Users\\Owner\\Desktop\\Thunberg,Dametre\\5-29 Data and movies\\New folder (2)\\Data 2.csv", delimiter=",", skiprows=1, unpack=True)
plt.plot(area,pressure, label='area/pressure!',color='b')

plt.xlabel('area', color='b')
plt.ylabel('Pressure', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()

ax2.set_ylabel('Intensity (measured by average pixel value)', color='r')

ax2.tick_params('y', colors='r')

ax2.plot(area,pixel, color='r')

plt.title('Kibron Trough Pressure-Area-Intensity Graph')
plt.legend()
plt.show()
It is in the good direction but you are mixing 2 methods and reading the same file twice for no reason.
First you read the file with pandas, and later you read it again directly.
In pandas you cut at the desired row... but forgot to do something with the result. For the data read with numpy, you plot it right, but you are not throwing away the data after the minimum area.
Last, you mix the pyplot style plotting (typical when working from a command line) with the "OOP" style for matplotlib (better when writing scripts)

I just cleaned up the version using numpy, if you prefer to use pandas, just change the line of the loadtxt and the references to pressure, area... with df.pressure, df.area and so on.
import matplotlib.pyplot as plt
import numpy as np

# Read the input data only once
area, pressure, pixel = np.loadtxt ("data.csv", delimiter=",", skiprows=1, unpack=True)

# Find the minimum index
rowmin = area.argmin()

# Ignore completely after the desired index...
area = area[:rowmin+1]
pressure = pressure[:rowmin+1]
pixel = pixel[:rowmin+1]

# Fine control of the matplotlib, you can use GridSpec to do fancy graphs...
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

# Plot in the first axis
ax1.plot(area, pressure, label='area/pressure!', color='b')

# And decorate it
ax1.set_xlabel('area', color='b')
ax1.set_ylabel('Pressure', color='b')
ax1.tick_params('y', colors='b')

# Now plot in the secondary axes
ax2 = ax1.twinx()
 
ax2.plot(area, pixel, color='r')
ax2.set_ylabel('Intensity (measured by average pixel value)', color='r')
ax2.tick_params('y', colors='r') 

#
ax1.set_title('Kibron Trough Pressure-Area-Intensity Graph')
ax1.legend()

plt.show()
Hey there! that worked great! thank you very much! If you have another moment I was wondering how I could add a second label to the legend? I have attempted to add the "label=" to the
ax2.plot(area, pixel, color='r')
since the first one is formatted like that. Would I have to Add a second label to the first text? or assign a specific coordinate for the new one?