Matplotlib 绘图与可视化 一些属性和错误

属性

  *)调整图像边缘及图像间的空白间隔plt.subplots.adjust(6个参数)

  图像外部边缘的调整可以使用plt.tight_layout()进行自动控制,此方法不能够很好的控制图像间的间隔.如果想同时控制图像外侧边缘以及图像间的空白区域,使用命令:

plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8, top=0.8,hspace=0.2, wspace=0.3)

  *)subplot(111)参数为111,即中间没有逗号隔开的意思。

  参考链接:https://blog.youkuaiyun.com/S201402023/article/details/51536687

  

 #引入对应的库函数
import matplotlib.pyplot as plt
from numpy import *

#绘图
fig = plt.figure()
ax = fig.add_subplot(349)
ax.plot(x,y)
plt.show()

  其中,参数349的意思是:将画布分割成3行4列,图像画在从左到右从上到下的第9块

  那第十块怎么办,3410是不行的,可以用另一种方式(3,4,10)。

  如果一块画布中要显示多个图怎么处理?

import matplotlib.pyplot as plt
from numpy import *

fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.plot(x,y)
ax = fig.add_subplot(2,2,3)
ax.plot(x,y)
plt.show()

  

错误

*)funcAnimation()中frags向帧函数传递附加参数时格式不对提示错误TypeError: update_insert() takes 2 positional arguments but 10 were given

正确格式:

anim=animation.FuncAnimation(plt.fig,update_insert,init_func=None,repeat=False,frames=np.arange(0,6 ),interval=2000,fargs=(collection))

def update_insert(i,*collection):
    global ax,X
    print(collection) 
    --snip--

  

*)由于scatter()中参数不规范引起的错误

参考链接:https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.scatter.html?highlight=scatter#matplotlib.pyplot.scatter

标记颜色。可能的值:

  • 单色格式字符串。
  • 一系列长度为n的颜色规格。
  • 使用cmap和 norm映射到颜色的n个数字序列。
  • 一个二维数组,其中行是RGB或RGBA。

请注意,c不应该是单个数字RGB或RGBA序列,因为它与要进行颜色映射的值数组无法区分。如果要为所有点指定相同的RGB或RGBA值,请使用具有单行的二维数组。否则,在大小与x 和y匹配的情况下,值匹配将具有优先权。

默认为None。在这种情况下,标记的颜色是由的值来确定colorfacecolorfacecolors。如果未指定或None标记颜色,则标记颜色由Axes“当前”形状的下一个颜色确定并填充“颜色循环”。此周期默认为rcParams["axes.prop_cycle"]

        ax.scatter(X1,X1,c=b)
        ax.scatter(X2,X2,c=b,s=50)
        ax.scatter(X1,X1,c=g)

  会报错:

(sort) λ python matplotlib_learn.py
[1, 2, 3, 4, 5]
Traceback (most recent call last):
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process
    func(*args, **kwargs)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\animation.py", line 953, in _start
    self._init_draw()
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\animation.py", line 1732, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\animation.py", line 1755, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "matplotlib_learn.py", line 184, in update_insert
    ax.scatter(X2,X2,c=b,s=50)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\__init__.py", line 1589, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 4446, in scatter
    get_next_color_func=self._get_patches_for_fill.get_next_color)
  File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 4257, in _parse_scatter_color_args
    n_elem = c_array.shape[0]
IndexError: tuple index out of range

  

转载于:https://www.cnblogs.com/Gaoqiking/p/11075384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值