屬性
*)在使用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
