Issue
I have to make a mass histogram of animals with a dataframe on pandas. The goal is that on my x-axis, I have the different masses of my CSV file, and on my y-axis, I have the number of animals that have that mass. I am a beginner in this field and I need to make a simple and understandable code
Here is my current code :
`
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df = pd.read_csv("S:\Annee1\ISD\TP3\PanTHERIA_1-0_WR05_Aug2008.txt", sep='\t')
ax = (df
.loc[:, ['5-1_AdultBodyMass_g', 'MSW05_Binomial']]
.groupby('MSW05_Binomial')
.count('MSW05_Binomial')
.plot.bar(rot=45, figsize=(16, 8))
)
ax.set_title('Masses corporelles de tous les animaux', fontsize=14)
ax.set_xlabel('Animaux', fontsize=12)
ax.set_ylabel('Masse corporelle', fontsize=12)
`
Thanks a lot
PS: If you have any questions about my project, don't hesitate
Solution
You could use directly the pandas.DataFrame.hist
function.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("S:\Annee1\ISD\TP3\PanTHERIA_1-0_WR05_Aug2008.txt", sep="\t")
ax = plt.axes()
mass_column = "5-1_AdultBodyMass_g"
df[(df[mass_column] < 1e4) & (df[mass_column] > 0)].hist(column=mass_column, bins=100, ax=ax)
ax.set_title("Masses corporelles de tous les animaux", fontsize=14)
ax.set_xlabel("Masse corporelle (g)", fontsize=12)
ax.set_ylabel("Nombre d'animaux", fontsize=12)
Answered By - Clement Piat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.