博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 之 histogram直方图
阅读量:2135 次
发布时间:2019-04-30

本文共 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/

你可能感兴趣的文章
【LEETCODE】165-Compare Version Numbers
查看>>
【LEETCODE】299-Bulls and Cows
查看>>
【LEETCODE】223-Rectangle Area
查看>>
【LEETCODE】12-Integer to Roman
查看>>
【学习方法】如何分析源代码
查看>>
【LEETCODE】61- Rotate List [Python]
查看>>
【LEETCODE】143- Reorder List [Python]
查看>>
【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
查看>>
【LEETCODE】86- Partition List [Python]
查看>>
【LEETCODE】147- Insertion Sort List [Python]
查看>>
【算法】- 动态规划的编织艺术
查看>>
用 TensorFlow 让你的机器人唱首原创给你听
查看>>
对比学习用 Keras 搭建 CNN RNN 等常用神经网络
查看>>
深度学习的主要应用举例
查看>>
word2vec 模型思想和代码实现
查看>>
怎样做情感分析
查看>>
用深度神经网络处理NER命名实体识别问题
查看>>
用 RNN 训练语言模型生成文本
查看>>
RNN与机器翻译
查看>>
用 Recursive Neural Networks 得到分析树
查看>>