Python Pandas 箱线图的实现

yipeiwu_com5年前Python基础

各国家用户消费分布

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

 

data = {

  'China': [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2500],

  'America': [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100],

  'Britain': [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000],

  "Russia": [800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900]

}

df = pd.DataFrame(data)

 

# df.plot.box(title="Consumer spending in each country", vert=False)

df.plot.box(title="Consumer spending in each country")

 

plt.grid(linestyle="--", alpha=0.3)

plt.show()

  

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

 

data = {

  'China': [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2500],

  'America': [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100],

  'Britain': [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000],

  "Russia": [800, 1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900]

}

df = pd.DataFrame(data)

 

from pandas.plotting import table

 

fig, ax = plt.subplots(1, 1)

 

table(ax, np.round(df.describe(), 2),

   loc='upper right',

   colWidths=[0.1, 0.1, 0.1, 0.1]

   )

 

# df.plot.box(title="Consumer spending in each country", vert=False)

df.plot.box(title="Consumer spending in each country",

      ax=ax,

      ylim=(750, 3000))

 

plt.grid(linestyle="--", alpha=0.3)

plt.show()

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

 

data = {"gender": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],

    'China': [1000, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2500],

    'America': [1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100]

    }

df = pd.DataFrame(data)

 

# df.boxplot(column=["China", "America"], by="gender",vert=False)

df.boxplot(column=["China", "America"], by="gender")

 

plt.grid(linestyle="--", alpha=0.3)

plt.show()

  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现基于朴素贝叶斯的垃圾分类算法

python实现基于朴素贝叶斯的垃圾分类算法

一、模型方法        本工程采用的模型方法为朴素贝叶斯分类算法,它的核心算法思想基于概率论。我们称之为“朴素”,是因为整个形式化过程只做最原...

使用Python编写简单的端口扫描器的实例分享

使用Python编写简单的端口扫描器的实例分享

单线程实现 单线程实现道理比较简单,这里尝试Soket连接3389,连接成功说明端口开放,否则说明没有开远程服务。随便修改了一下就ok了,代码如下,最终得到自己的IP地址。 #!/u...

python读写配置文件操作示例

本文实例讲述了python读写配置文件操作。分享给大家供大家参考,具体如下: 在用编译型语言写程序的时候,很多时候用到配置文件,作为一个约定的规则,一般用 ini 文件作为配置文件,当然...

TensorFlow打印tensor值的实现方法

TensorFlow打印tensor值的实现方法

最近一直在用TF做CNN的图像分类,当softmax层得到预测结果后,我希望能够看到预测结果,以便和标签之间进行比较。特此补上,以便自己记忆。 我现在通过softmax层得到变量trai...

分析经典Python开发工程师面试题

你知道吗?实际上Python早在20世纪90年代初就已经诞生,可是火爆时间却并不长,就小编本人来说,也是前几年才了解到它。据统计,目前Python开发人员的薪资待遇为10K以上,这样的诱...