Python Pandas 箱线图的实现

yipeiwu_com6年前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虚拟环境的安装和配置(virtualenv,virtualenvwrapper)

python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)

一、为什么要安装虚拟环境     情景一、项目A需要某个库的1.0版本,项目B需要这个库的2.0版本。如果没有安装虚拟环境,那么当你使用这两个项目时,你就需要来回的卸...

一些Python中的二维数组的操作方法

需要在程序中使用二维数组,网上找到一种这样的用法:   #创建一个宽度为3,高度为4的数组 #[[0,0,0], # [0,0,0], # [0,0,0], # [0,...

Python入门篇之正则表达式

 正则表达式有两种基本的操作,分别是匹配和替换。 匹配就是在一个文本字符串中搜索匹配一特殊表达式; 替换就是在一个字符串中查找并替换匹配一特殊表达式的字符串。   1...

python3实现在二叉树中找出和为某一值的所有路径(推荐)

python3实现在二叉树中找出和为某一值的所有路径(推荐)

请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径。 规则如下: 1、从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设置的某一值的相同...

利用python实现简单的循环购物车功能示例代码

本文主要给大家介绍了关于python实现循环购物车功能的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 示例代码 # -*- coding: utf-8 -*- __a...