python中用matplotlib畫圖遇到的一些問題及解決
python用matplotlib畫圖遇到的一些問題
1.用plt畫直方圖時
在hist這句話報錯
ValueError: max must be larger than min in range parameter.
n, bins, patches = plt.hist(x,num_bins, facecolor=plt.rcParams['axes.color_cycle'][2], alpha=0.5,edgecolor='black',linestyle='-',linewidth=1)
后來發(fā)現(xiàn)是由于讀取的csv文件中存在NaN值造成的。
因為之前對它進行過增加數(shù)據(jù)行數(shù),但是恢復(fù)原值后沒有徹底刪除掉所增加的行,而只是把值刪除了,所以會報錯。
2.plt保存圖片時
生成的svg圖片下邊少了一條,顯示不完全,x軸的label只顯示了一半:
解決方法:
plt.savefig('D:\\weights_a.svg',format='svg' ,bbox_inches='tight') #保存為svg格式,再用inkscape轉(zhuǎn)為矢量圖emf后插入word中 bbox使保存圖片時沒白邊還能顯示完全加bbox這個參數(shù)即可。
3.import時報錯
/home/heyintao/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: UserWarning:This call to matplotlib.use() has no effect because the backend has alreadybeen chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot,or matplotlib.backends is imported for the first time.
源代碼為:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')修改為: 即可。
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as pltpython畫直方圖報錯
max must be larger than min in range parameter
使用plt.show()畫直方圖,報錯為max must be larger than min in range parameter。
不妨把空值去掉試試。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas||過濾缺失數(shù)據(jù)||pd.dropna()函數(shù)的用法說明
這篇文章主要介紹了Pandas||過濾缺失數(shù)據(jù)||pd.dropna()函數(shù)的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
Python深度學(xué)習(xí)理解pytorch神經(jīng)網(wǎng)絡(luò)批量歸一化
這篇文章主要是Python深度學(xué)習(xí)篇,通過示例的詳解讓大家更好的理解pytorch神經(jīng)網(wǎng)絡(luò)批量歸一化,有需要的的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

