美文网首页
5-matplotlib子图

5-matplotlib子图

作者: 蓝剑狼 | 来源:发表于2018-09-02 23:33 被阅读38次

在matplotlib中,整个图像为一个Figure对象
在Figure对象中可以包含一个或者多个Axes对象
每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域

plt.figure, plt.subplot

# plt.figure() 绘图对象
# plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, 
# frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, **kwargs)

fig1 = plt.figure(num=1,figsize=(4,4))
plt.plot(np.random.rand(50).cumsum(),'k--')
fig2 = plt.figure(num=2,figsize=(4,2))
plt.plot(50-np.random.rand(50).cumsum(),'k--')
# num:图表序号,可以试试不写或都为同一个数字的情况,图表如何显示
# figsize:图表大小

# 当我们调用plot时,如果设置plt.figure(),则会自动调用figure()生成一个figure, 严格的讲,是生成subplots(111)
图片.png
# 子图创建1 - 先建立子图然后填充图表

fig = plt.figure(figsize=(10,6),facecolor = 'gray')

ax1 = fig.add_subplot(2,2,1)  # 第一行的左图
plt.plot(np.random.rand(50).cumsum(),'k--')
plt.plot(np.random.randn(50).cumsum(),'b--')
# 先创建图表figure,然后生成子图,(2,2,1)代表创建2*2的矩阵表格,然后选择第一个,顺序是从左到右从上到下
# 创建子图后绘制图表,会绘制到最后一个子图

ax2 = fig.add_subplot(2,2,2)  # 第一行的右图
ax2.hist(np.random.rand(50),alpha=0.5,edgecolor='black')

ax4 = fig.add_subplot(2,2,3)  # 第二行的右图
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
ax4.plot(df2,alpha=0.5,linestyle='--',marker='.')
# 也可以直接在子图后用图表创建函数直接生成图表
图片.png
# 子图创建2 - 创建一个新的figure,并返回一个subplot对象的numpy数组 → plt.subplot

fig,axes = plt.subplots(2,3,figsize=(10,6),facecolor = 'gray')
ts = pd.Series(np.random.randn(1000).cumsum())
print(axes, axes.shape, type(axes))
# 生成图表对象的数组
# print(ts)
ax1 = axes[1,2]
ax1.plot(ts)
图片.png
# plt.subplots,参数调整

fig,axes = plt.subplots(2,2,sharex=True,sharey=True,figsize=(13,6))
# sharex,sharey:是否共享x,y刻度

for i in range(2):
    for j in range(2):
        axes[i,j].hist(np.random.randn(500),color='r',alpha=0.5,edgecolor='b')
plt.subplots_adjust(wspace=0,hspace=0)
# wspace,hspace:用于控制宽度和高度的百分比,比如subplot之间的间距
图片.png
# 子图创建3 - 多系列图,分别绘制

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot(style = '--.',alpha = 0.4,grid = True,figsize = (14,10),
       subplots = True,
       layout = (2,3),
       sharex = False)
plt.subplots_adjust(wspace=0,hspace=0.2)
# plt.plot()基本图表绘制函数 → subplots,是否分别绘制系列(子图)
# layout:绘制子图矩阵,按顺序填充
图片.png

相关文章

  • 5-matplotlib子图

    在matplotlib中,整个图像为一个Figure对象在Figure对象中可以包含一个或者多个Axes对象每个A...

  • Python学习笔记5-matplotlib

    matplotlib 学习莫烦python,非常感谢~记录自己在学习python过程中的点滴。 matplotli...

  • 子图

    子图是一个人,一个在我的生命中曾经来过的一个人,他是一个很奇怪的人,我偶然遇到,他好像能读懂我的心声,我们之间不需...

  • matplotlibmatplotlib 数据可视化分析

    matplotlib数据可视化分析 直方图 子图功能 什么是子图功能呢?子图就是在一个大的画板里面能够显示多张小图...

  • 极大连通子图 极小连通子图 连通分量

    有向图不存在极小连通子图的概念 连通图的生成树为该连通图的一个极小连通子图

  • 数据结构-学习二

    图: 无向图,有向图度,子图,路径,环,连通图,连通子图。 存储: 邻接矩阵二维数组。 邻接表+数组加链表优先搜...

  • 高峰体验@国画中的百子图

    百子图,我们也叫它百子迎福图、百子嬉春图、百子戏春图。在中国传统文化中有它的一种特定含义。由于“百”含有大或者无穷...

  • 百子图价值几何

    百子图,我们也叫它百子迎福图、百子嬉春图、百子戏春图。在中国传统文化中有它的一种特定含义。由于"百"含有大或者无穷...

  • 恰如其分的小确幸!

    图/子玉

  • 冬至前夜

    图/子陌

网友评论

      本文标题:5-matplotlib子图

      本文链接:https://www.haomeiwen.com/subject/xjfvwftx.html