Python Forum
Dropping all rows of multiple columns after the max of one cell
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dropping all rows of multiple columns after the max of one cell
#1
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()
Reply
#2
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()
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging rows and adding columns based on matching index pythonnewbie78 3 811 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,216 Aug-03-2020, 02:05 AM
Last Post: scidam
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 2,155 Jun-09-2020, 06:05 AM
Last Post: Pan
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,458 Mar-14-2020, 09:19 AM
Last Post: scidam
  read_csv error and rows/columns missing karlito 9 5,321 Nov-11-2019, 06:48 AM
Last Post: karlito
  Drop rows if a set of columns has a value dervast 1 1,984 Sep-12-2019, 04:18 PM
Last Post: sd_0912
  Dropping a column from pandas dataframe marco_ita 6 15,320 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  display graph in columns and rows william888 1 1,843 Jul-02-2019, 10:19 AM
Last Post: dataman
  Grab columns from multiple files, combine into one jon0852 0 2,017 Feb-12-2019, 02:53 AM
Last Post: jon0852
  Slicing String cell by cell Vigneshkumarsakthivel 0 2,394 Sep-02-2018, 05:59 PM
Last Post: Vigneshkumarsakthivel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020