Issue
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.
Solution
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()
df.set_index('Species').loc[['AGREP', 'EUPESU']].plot()
Answered By - Jianxun Li
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.