1

I have a pandas DataFrame similar to :

Species  Count1 Count2 
AGREP      2       10
GYLEP      4       6
POAPRA     2       8
EUPESU     1       11

and I want to make a line plot of species vs. Count 1 with the x-axis being the species and the y-axis as Count 1.

I try to use the code:

import matplotlib.pyplot as plt
plt.plot(df.Species,df.Count1)

but this returns the error:

ValueError: could not convert string to float

I want to eventually only chose certain species to plot as well against both counts. For example, I want to plot AGREP and EUPESUS on the x axis against both Count 1 and Count 2 as the y axis. Again though, it won't plot the string as the axis name.

2 Answers 2

1

You can set those strings as index and just use the pandas.DataFrame .plot method.

import pandas as pd

# your data
# ===================================

df


  Species  Count1  Count2
0   AGREP       2      10
1   GYLEP       4       6
2  POAPRA       2       8
3  EUPESU       1      11


# plot
# ===================================
df.set_index('Species').plot()

enter image description here

df.set_index('Species').loc[['AGREP', 'EUPESU']].plot()

enter image description here

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

Comments

0

You can a set a specific column in your plot like this

import pandas as pd  
  
# assign data of lists.  
data = {'Species': ['AGREP', 'GYLEP', 'POAPRA', 'EUPESU'],
 'Count1': [2, 4, 2, 1],
 'Count2': [10, 6, 8, 11]}  
  
# Create DataFrame  
df = pd.DataFrame(data)  
df

# Output
  Species  Count1  Count2
0   AGREP       2      10
1   GYLEP       4       6
2  POAPRA       2       8
3  EUPESU       1      11

# Plot Count1 column while index is Species
df.set_index('Species')['Count1'].plot()

Plot Count1 column while index is Species: img

Comments

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.