iris.csv
Histogram
import matplotlib.pyplot as plt import pandas as pd iris_df = pd.read_csv("iris.csv") iris_df["sepallength"].hist(by=iris_df["class"]) #Group data by class label plt.show()
Stacked histogram
堆叠直方图
import matplotlib.pyplot as plt import pandas as pd iris_df = pd.read_csv("iris.csv") labels = iris_df["class"].unique() #get all class labels #Extract attribute values with the specific label def filterData(df, attribute, label): values = iris_df[iris_df["class"] == label][[attribute]].values #Convert to NumPu array return pd.DataFrame(data=values, columns=[label])#Return as DataFrame with a single column, using class label column name df = pd.concat([filterData(iris_df, "sepallength", label) for label in labels], axis="columns") #Combine data of different class labels into a table df.plot.hist(stacked=True, bins=5) plt.show()
Stacked histogram using Seaborn
import matplotlib.pyplot as plt import pandas as pd import seaborn as sns iris_df = pd.read_csv("iris.csv") sns.histplot(iris_df, x="sepallength", hue="class", multiple="stack") plt.show()
All histograms
import matplotlib.pyplot as plt import pandas as pd import seaborn as sns iris_df = pd.read_csv("iris.csv") labels = iris_df['class'].unique() columns = [column for column in iris_df.columns if column not in ['id', 'class']] #Get all attributes except id and class labels _, axes = plt.subplots(1, len(columns)) #Prepare plot areas for c in range(len(columns)): sns.histplot(iris_df, x=columns[c], hue="class", multiple="stack", ax=axes[c]) #Plot each histogram at the correct area plt.show()
Python 之 histogram直方图(pandas, pd)
于 2020-09-29 23:17:31 首次发布