Python可视化之pandas

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


1.解决坐标轴刻度负号乱码

复制代码
import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus']=False

2.解决中文乱码问题

复制代码
# 以下方式二选一
plt.rcParams['font.sans-serif']=['Simhei']
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] 

3.折线图Series.plot()&DataFrame.plot()

  • Series.plot():
    kind:line(折线图),bar(垂直条形图)、barh(水平条形图)、hist(直方图)、box(箱型图)、kde(核密度估计图)与density相同,area(面积图)、pie(饼图)

    da = pd.Series([1,5,3,4,5,2.2,7,6.5])
    da.index.name='site'
    da.plot(kind='line',linestyle='-.',color='c',marker='*')

  • DataFrame.plot():
    kind:line(折线图,默认),bar或barh(条形图)、hist(频率柱状图)、box(箱型图)、kde(密度图,需引用scipy包)与density相同,area(区域图,不同区域面积占比)、pie(饼图)、scatter(散点图)、hexbin()

    da = pd.DataFrame({'A':[3,5,3,4,5,5,7,6.5],'B':[2,6,2,1,7,2.4,6,8.3]})
    da.plot(kind='line',linestyle='-',color='c',marker='o',
    xticks=[2,4,6,8,10],yticks=[2,4,6,8,10],xlim=[-1,9],title='对比折线图')

4.条形图

复制代码
da = pd.Series([1,5,3,4,5,2.2,7,6.5])
da.index=['a','b','c','d','e','f','g','h']
da.index.name='site'
da.plot(kind='bar',fontsize=12)
复制代码
da = pd.DataFrame({'A':[3,5,3,4,5,5,7,6.5],'B':[2,6,2,1,7,2.4,6,8.3]})
da.index=['a','b','c','d','e','f','g','h']
da.index.name='site'
da.plot(kind='bar',fontsize=12)

直方图是一种可以对值频率进行离散化显示的柱状图。数据点被拆分到离散的、间隔均匀的面元中,绘制的是各面元中数据点的数量,bins=20表示数值分辨率,具体来说是将随机数设定一个范围

复制代码
a = np.random.randn(100)

df = pd.DataFrame({'length':a})
df.plot.hist(bins=20)
复制代码
df = pd.DataFrame({'a':np.random.randn(100)+1,'b':np.random.randn(100),'c':np.random.randn(100)-1},
                  index=range(1,101),columns=['a','b','c'])

df.plot.hist()


下图是对数值进行累加,并绘制横向直方图,横轴表示频率,cumulative=True表示将频率从大到小排列

df.diff().hist()的效果是将DataFrame当中column分开,即将a,b,c分开绘制成三张图,df.diff().hist()可达到这个效果

5.箱线图

线条从上到下分布表示:最小值,第一四分位、中位数、第三四分和最大值

  • 绘制方法:
    Series.plot.box()
    DataFrame.boxplot()
    DataFrame.plot.box()

  • 参数:
    boxes:盒身
    whiskers:盒须
    medians:中位数
    caps:最大值,最小值

    df.plot.box(color=dict(boxes='c',whiskers='r',medians='b',caps='g'),sym='r+') #sym设置极端值样式

水平箱线图

6.区域面积图(堆积折线图)

复制代码
np.random.seed(80)
a=np.random.rand(10,5)

df = pd.DataFrame(a,columns=['a','b','c','d','e'])

df.plot.area()#生成堆积图


7.散点图

复制代码
np.random.seed(80)
a=np.random.rand(10,5)

df = pd.DataFrame(a,columns=['a','b','c','d','e'])

df.plot.scatter(x='b',y='a')

图形嵌套

8.饼图

Series.plot.pie()

DataFrame.plot.pie()

复制代码
np.random.seed(80)

df = pd.Series(np.random.rand(5),index=['a','b','c','d','e'],name='series')
df.plot.pie(figsize=(6,6))
复制代码
np.random.seed(80)

df = pd.DataFrame(np.random.rand(5,2),index=['a','b','c','d','e'],columns=['x','y'])
df.plot.pie(subplots=True, figsize=(12,6))
相关推荐
l12586517 分钟前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
Littlehero_1211 小时前
QT自定义控件之热换站系统(源码开源)
开发语言·qt
meilindehuzi_a1 小时前
Python 基础语法(1):常量、变量、类型、输入输出与运算符
开发语言·python
caimouse1 小时前
ReactOS 窗口系统分析(5):可见区域与窗口 DC — vis.c + windc.c
c语言·开发语言
Zane19941 小时前
调用了 async 函数却没执行?一文讲透协程、event loop 与 await
后端·python
OPEN-F2 小时前
Python进阶教程:装饰器与生成器深入
开发语言·python
zhipujiaoyu2 小时前
2026年大数据专科何去何从?就业前景、发展趋势全揭秘!
大数据·python
l1258652 小时前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
zoujiahui_20182 小时前
别再只写 np.random.seed() 了:default_rng 与 np.random、random 到底差在哪?
python
Patrick在香港2 小时前
Claude Agent 进阶:4 种工具调用编排模式,让 0820 那 13 个 endpoint 并行起来
python·ai·ai编程