Matplotlib 绘图与可视化 一些属性和错误
属性
*)在使用animate方法中若让interval=0,则会直接输出最后一帧
*)清除图像 包括这个方法到底是图形区的对象调用的还是画布对象调用的?
这个莫名其妙就起作用了
来源连接:https://codeday.me/bug/20170309/5150.html
def update_insert(i):
global ax,X
plt.cla()#要以绘图区的对象调用的方法吧
# plt.close()#这里又能关闭了
# plt.clf()#清楚figure里的内容
X=np.random.randint(0,100,10)
ax.scatter(X,X)
plt.xticks([]),plt.yticks([])
return ax,X
*)调整图像边缘及图像间的空白间隔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.csdn.net/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()
错误
*)c:\users\administrator.sc-201605202132\appdata\local\programs\python\python36\Lib\tkinter\__init__.py:1705: UserWarning: Tight layout not applied. The left and right margins cannot be made large enough to accommodate all axes decorations.
axs=fig.add_subplot(111)
axs.set_xlim(0,7)
axs.set_ylim(0,5)
text= axs.text(0.02,0.90,'test',transform=axs.transAxes)
text.set_backgroundcolor('r')
text.set_position((0.9,.9))#不能超过1,和上面的设置是一样的
*)matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ['bubble_sort', 'bidirectional_bubble_sort']
xticks=[d.__name__ for d in algorithm_list]
print(xticks)
axs.set_xticks(xticks)#因为是字符串数组,所以不能,应该是数字
#xticks
['bubble_sort', 'bidirectional_bubble_sort']
*)ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
#错误的
spend_time=[1,2]
axs.set_yticks([spend_time])
#True
axs.set_yticks(spend_time)
*)axs[i].set_xticks([]) TypeError: 'list' object is not callable
参考链接:https://stackoverflow.com/questions/46231439/problems-with-matplotlib-pyplot-xticks(见回答2)
原因:是因为起那面已经有过设置x轴标记为空的了
for i in range(algorithm_num):
frames_names[algorithm_list[i].__name__]=[]
#顺便对画布进行设置
axs.append(fig.add_subplot(121+i))
# axs[-1].set_xticks=([])#这里已经有过了,将这个注释掉
# axs[-1].set_yticks=([])
#顺便运行函数了
frames_names[algorithm_list[i].__name__]=algorithm_list[i](copy.deepcopy(original_data_object))
plt.subplots_adjust(left=0.05,right=0.95,bottom=0.1,top=0.90,wspace=0.1,hspace=0.2) #寻找最大帧,算了,还是把所有的帧数都存到一个dict里面吧,但是dict好像不能向里面添加
frame_count={}
for i in range(algorithm_num):
frame_count['{}'.format(algorithm_list[i])]=str(len(frames_names[algorithm_list[i].__name__]))
def animate(fi):
bars=[]
for i in range(algorithm_num):
if len(frames_names[algorithm_list[i].__name__])>fi:
axs[i].cla() axs[i].set_xticks([])
axs[i].set_yticks([])
axs[i].set_title(str(algorithm_list[i].__name__))
bars+=axs[i].bar(list(range(Data.data_count)),
[d.value for d in frames_names[algorithm_list[i].__name__][fi]],
1,
color=[d.color for d in frames_names[algorithm_list[i].__name__][fi]],
).get_children()
return bars
*)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
。在这种情况下,标记的颜色是由的值来确定color
,facecolor
或facecolors
。如果未指定或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
*)ValueError: shape mismatch: objects cannot be broadcast to a single shape错误:
可能是因为传入的两个参数之间不是一一对应的,类似于因为长度的原因,一个参数中的某些数据不能和另一个参数中的数据一起被使用。比如在画图的时候
bars+=ax.bar(list(range(0,Data.data_count)),#我在创建数据的时候搞错了,这里是16个,而下面的是17个
[d.value for d in frames[fi]],
1,
color=[d.color for d in frames[fi]]
).get_children()
*)类中的类似变量名错误的错误不会提示啊,哦哦好像是别的地方就写错了
def set_color(self,ragb=None):#这里也写错了
if not ragb:#这里也写错了
rgba=(0,#但是这个这么没有提示
1-self.value/(self.data_count*2),
self.value/(self.data_count*2)+0.5,
1)
self.color=ragb
---snip---
d=Data(2)
print(d.color) #输出
(sort) λ python Visualization_bubble_sort.py
None
Matplotlib 绘图与可视化 一些属性和错误的更多相关文章
- Matplotlib 绘图与可视化 一些控件的介绍和属性,反正就是乱七八糟的
这个链接里有下面这个图(图里还有超链接):https://matplotlib.org/3.1.1/api/artist_api.html#matplotlib.artist.Artist 各种图例: ...
- python金融与量化分析------Matplotlib(绘图和可视化)
-----------------------------------------------------------Matplotlib:绘图和可视化------------------------ ...
- Py修行路 Matplotlib 绘图及可视化模块
Matplotlib是一个强大的Python绘图和数据可视化的工具包. 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...
- matplotlib绘图基本用法-转自(http://blog.csdn.net/mao19931004/article/details/51915016)
本文转载自http://blog.csdn.net/mao19931004/article/details/51915016 <!DOCTYPE html PUBLIC "-//W3C ...
- python实战学习之matplotlib绘图续
学习完matplotlib绘图可以设置的属性,还需要学习一下除了折线图以外其他类型的图如直方图,条形图,散点图等,matplotlib还支持更多的图,具体细节可以参考官方文档:https://matp ...
- python中利用matplotlib绘图可视化知识归纳
python中利用matplotlib绘图可视化知识归纳: (1)matplotlib图标正常显示中文 import matplotlib.pyplot as plt plt.rcParams['fo ...
- Matplotlib:绘图和可视化
Matplotlib:绘图和可视化 简介 简单绘制线形图 plot函数 支持图类型 保存图表 一 .简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分 ...
- 基于matplotlib的数据可视化 - 笔记
1 基本绘图 在plot()函数中只有x,y两个量时. import numpy as np import matplotlib.pyplot as plt # 生成曲线上各个点的x,y坐标,然后用一 ...
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识
第1节 pandas 回顾 第2节 读写文本格式的数据 第3节 使用 HTML 和 Web API 第4节 使用数据库 第5节 合并数据集 第6节 重塑和轴向旋转 第7节 数据转换 第8节 字符串操作 ...
随机推荐
- fs
yum install -y make expat-devel git gcc-c++ autoconf automake libtool wget python-devel ncurses-deve ...
- bilibili用户信息查询
bilibili用户信息查询 http://space.bilibili.com/ajax/member/GetInfo?mid= 后缀为用户mid号 # -*- coding:utf-8 -*- # ...
- mysql基础综述(四)
1.数据库的简单介绍 1.1 数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle 是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公司 ...
- 剑指offer面试题14(Java版):调整数组顺序使奇数位于偶数的前面
题目:输入一个整数数组.实现一个函数来调整该数组中数字的顺序.使得全部奇数位于数组的前半部分.全部偶数位于数组的后半部分. 1.基本实现: 假设不考虑时间复杂度,最简单的思路应该是从头扫描这个数组,每 ...
- jdk7与jdk8环境共存与切换
1,先安装jdk7,配置环境变量JAVA_HOME,然后安装jdk8. 2,安装jdk8后,JAVA_HOME指向未做修改,执行java -version显示还是以前的jdk7版本信息, 3,接下来我 ...
- python中在py代码中如何去调用操作系统的命令
import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',10800)) sk.listen() conn,a ...
- ROS-SLAM-自主导航
前言:无. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_exploring.git 一.启动仿真环境 cd ~/ca ...
- ROS安装教程
对于ROS的安装,在它的官方网站: http://wiki.ros.org/ROS/Installation 中也有详细说明.但是对于像博主这样先天英语发育不全的人来说,直接看官网还是有点困难的. 所 ...
- Js radio
<input type="radio" name="sex" value="1" />男 <input type=&quo ...
- 关于getElementsByTagName的遍历顺序
关于getElementsByTagName的遍历顺序是怎么样的呢? getElementsByTagName的遍历顺序是从HTML的页面从上到下遍历还是按照标签的嵌套顺序层层遍历的呢? 来做个小小的 ...