Python Pandas 箱线图的实现

时间:2021-05-22

各国家用户消费分布

import numpy as npimport pandas as pdimport 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 npimport pandas as pdimport 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 npimport pandas as pdimport 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()

  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章