目录

前言

一、pandas matplotlib是什么?

二、实现一个简单的直方图

1.基本实现一个直方图

2.展示图

 三.分析

1.如果我们想测试在一周内的消费怎么办?

2.如何将一周内的消费记录用直方图展示出来?

3.解决代码冗余

4.图展示

总结

前言

Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。

一、pandas matplotlib是什么?

pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。

二、实现一个简单的直方图

1.基本实现一个直方图

代码如下:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import rcParams, font_manager

rcParams['axes.unicode_minus'] = False

rcParams['font.family'] = 'SimHei'

# my_font = font_manager.FontProperties(fname="C:/windows/Fonts/simsun.ttc")

# 准备-- 一天的基本数据

consume=["购物","娱乐","学习","饮食"]

price=[98,37.8,10.0,22.0]

plt.bar(x=consume,height=price,label="消费记录")

plt.legend(loc=0)

plt.show()

2.展示图

 三.分析

1.如果我们想测试在一周内的消费怎么办?

使用pandas的date_range,这个方法可以有效地创建一个时间序列。

可是使用pandas的DataFrame,我们必须使用行,列,值来完善这个对象。

因此,在这里,我们使用date_range作为column,消费类型作为index

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import rcParams, font_manager

rcParams['axes.unicode_minus'] = False

rcParams['font.family'] = 'SimHei'

date_range = pd.date_range(

start='2023-01-01',periods=7

)

consume=["购物","娱乐","学习","饮食"]

# 注意:

price=[[90,20,87,122,35,56,12],

[19,87,76,18,65,76,32],

[27,16,23,38,65,6,12],

[23,19,32,28,65,56,2]]

pdf=pd.DataFrame(data=price,columns=date_range,index=consume)

# print(pdf)

index=pdf.index

col=pdf.columns

2.如何将一周内的消费记录用直方图展示出来?

2.1 问题:平铺展示

如果想要平铺展示,只需要设定每个直方图的宽度的起始就行了。第一个直方图宽度起始位置是0,宽度为0.1,第二个起始位0.1,宽度为0.1,那么,依次是

宽度起始宽度00.10.10.10.20.10.30.1

width=0.1

plt.bar([i+width*0 for i in range(len(index))], pdf["2023-01-0"+str(1)], width=width, label="2023-01-0"+str(1))

plt.bar([i+width*1 for i in range(len(index))], pdf["2023-01-0"+str(2)], width=width, label="2023-01-0"+str(2))

plt.bar([i+width*2 for i in range(len(index))], pdf["2023-01-0"+str(3)], width=width, label="2023-01-0"+str(3))

plt.bar([i+width*3 for i in range(len(index))], pdf["2023-01-0"+str(4)], width=width, label="2023-01-0"+str(4))

plt.bar([i+width*4 for i in range(len(index))], pdf["2023-01-0"+str(5)], width=width, label="2023-01-0"+str(5))

plt.bar([i+width*5 for i in range(len(index))], pdf["2023-01-0"+str(6)], width=width, label="2023-01-0"+str(6))

plt.bar([i+width*6 for i in range(len(index))], pdf["2023-01-0"+str(7)], width=width, label="2023-01-0"+str(7))

plt.legend(loc=0)

plt.show()

2.2 问题:横坐标刻度的设定 (xticks)

plt.xticks([x+width*2 for x in range(4)], index) 

3.解决代码冗余

我们发现,就算解决了问题,代码冗余使得维护异常困难。那么如何使其适宜阅读呢?

将以上的plt.bar(...)代码修改为以下代码

for i in range(7):

plt.bar ([j + width * i for j in range (len (index))], pdf["2023-01-0" + str (i+1)], width=width, label="2023-01-0" + str (i+1))

4.图展示

四 完整代码 (index我用xxx代替了,可以替换成别的数据)

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import rcParams, font_manager

rcParams['axes.unicode_minus'] = False

rcParams['font.family'] = 'SimHei'

# my_font = font_manager.FontProperties(fname="C:/windows/Fonts/simsun.ttc")

# 准备-- 一天的基本数据

# consume=[xx,xx,xx,xx]

# price=[98,37.8,10.0,22.0]

# plt.bar(x=consume,height=price,label="xxxx")

# plt.legend(loc=0)

# plt.show()

date_range = pd.date_range(

start='2023-01-01',periods=7

)

consume=[xx,xx,xx,xx,xx]

# 注意:

price=[[90,20,87,122,35,56,12],

[19,87,76,18,65,76,32],

[27,16,23,38,65,6,12],

[23,19,32,28,65,56,2]]

pdf=pd.DataFrame(data=price,columns=date_range,index=consume)

# print(pdf)

index=pdf.index

col=pdf.columns

width=0.1

plt.bar([i+width*0 for i in range(len(index))], pdf["2023-01-0"+str(1)], width=width, label="2023-01-0"+str(1))

plt.bar([i+width*1 for i in range(len(index))], pdf["2023-01-0"+str(2)], width=width, label="2023-01-0"+str(2))

plt.bar([i+width*2 for i in range(len(index))], pdf["2023-01-0"+str(3)], width=width, label="2023-01-0"+str(3))

plt.bar([i+width*3 for i in range(len(index))], pdf["2023-01-0"+str(4)], width=width, label="2023-01-0"+str(4))

plt.bar([i+width*4 for i in range(len(index))], pdf["2023-01-0"+str(5)], width=width, label="2023-01-0"+str(5))

plt.bar([i+width*5 for i in range(len(index))], pdf["2023-01-0"+str(6)], width=width, label="2023-01-0"+str(6))

plt.bar([i+width*6 for i in range(len(index))], pdf["2023-01-0"+str(7)], width=width, label="2023-01-0"+str(7))

# for i in range(7):

# plt.bar ([j + width * i for j in range (len (index))], pdf["2023-01-0" + str (i+1)], width=width,

# label="2023-01-0" + str (i+1))

# print(i)

# plt.xticks([x+width*2 for x in range(4)], index)

plt.legend(loc=0)

plt.show()

# print(index,col)

总结

提示:这里对文章进行总结: 例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: