0

So, I've made a graph that plots multiple rows from a .csv file onto one graph. I want to make another graph that only includes specific rows to better analyze them.

My data is (only a portion of the total):

Number  Base-BW  1-BW    2-BW   3-BW    4-End-BW
182-14  39.7    41.1    40.3    39.5    38.8
95-14   43.3    41      41.9    42.4    41.6
15-14   59.4    59.4    59.1    59.1    56.4
124-14  76.4    77.4    74.8    74.5    68.1
183-14  35      35.5    36.8    37.3    35.4

My current script is:

# import all needed modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Set some Pandas options
import matplotlib.colors as cl
cl.Colormap('hsv')
plt.plot([0,1,2,3,4], (df['Base-BW'],df['1-BW'],df['2-BW'],df['3-BW'],df['4-End-BW']))
plt.axis([0, 4, 0, 100])
plt.xlabel('Day')
plt.ylabel('Weight (g)')
plt.title('Body Weight')
plt.legend(df['Number'], bbox_to_anchor=(1.1, 1.01))
plt.show()

Which yields:

enter image description here

So is there a way to just show, say, row1 (182-14), 3 (15-14), and 5 (183-14)? Or maybe just to graph those specific rows? I've checked other questions, and none pertain to using a .csv file like I am.

4
  • might help, this answer Commented Feb 27, 2015 at 21:13
  • 1
    You're not showing entirety of your code, what is SelectBreed how exactly do you read in your data? (I don't see a function or a csv reader that does that for you). Generally there is no universal function that does that. Your best bet is to read in csv file and store each line in a list, i.e. line, as a list and then plot it like plt.plot(x_values, line[i] where x values seem to be days (although your code plots a [1,2,3,4] as days. I might be missing something, but it doesn't look like that code should produce your result. Then again it depends a lot on what SelectBreed is. Commented Feb 27, 2015 at 21:18
  • I updated it to include the entire code, I didn't know if that was necessary or not. Still kinda new to python. Commented Feb 27, 2015 at 21:22
  • 1
    If you want us to test your data, making it as easy as possible to reconstitute it on our machines will help -- usually you can write out a section of the data structure as a string that can be re-read without, e.g., saving a file to disk. Which is a pain. Commented Feb 27, 2015 at 21:24

1 Answer 1

1

The Pandas library rocks for reading .csv files and then analyzing them. Using a sample dataset from R,

import pandas as pd
cw = pd.read_csv('ChickWeight.csv')
cw.columns

Out[166]: Index([u'weight', u'Time', u'Chick', u'Diet'], dtype='object')

cw[cw.Chick==1].plot('Time','weight')

enter image description here

Picking apart that last line: cw[cw.Chick==1] is all rows of cw for which Chick==1; pandas dataframes have a plot method (which is matplotlib, I think, so will be familiar).

You can also do complicated selections from the dataframe, there are some good related pandas questions.

edited to add: use a list of values to select rows from a pandas dataframe is probably the most literal approach to your question; Selecting rows from a Pandas dataframe with a compound (hierarchical) index is pretty cool for doing selections based on more than one column (comes up all the time in multi-factor experiments).

Sign up to request clarification or add additional context in comments.

3 Comments

The out for the cw.columns didn't show, is there a way you can edit it? Thank you.
Thank you, I'll check all of those out!
... The result in this answer, or when you ran a similar script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.