本文共 1620 字,大约阅读时间需要 5 分钟。
iris.csv
Histogram
import matplotlib.pyplot as pltimport pandas as pdiris_df = pd.read_csv("iris.csv")iris_df["sepallength"].hist(by=iris_df["class"]) #Group data by class labelplt.show()
Stacked histogram
堆叠直方图
import matplotlib.pyplot as pltimport pandas as pdiris_df = pd.read_csv("iris.csv")labels = iris_df["class"].unique() #get all class labels#Extract attribute values with the specific labeldef 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 namedf = pd.concat([filterData(iris_df, "sepallength", label) for label in labels], axis="columns") #Combine data of different class labels into a tabledf.plot.hist(stacked=True, bins=5)plt.show()
Stacked histogram using Seaborn
import matplotlib.pyplot as pltimport pandas as pdimport seaborn as snsiris_df = pd.read_csv("iris.csv")sns.histplot(iris_df, x="sepallength", hue="class", multiple="stack")plt.show()
All histograms
import matplotlib.pyplot as pltimport pandas as pdimport seaborn as snsiris_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 areasfor 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 areaplt.show()
转载地址:http://xaygf.baihongyu.com/