av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題

瀏覽:19日期:2022-07-25 11:45:13

在繪制正負(fù)樣本在各個特征維度上的CDF(累積分布)圖時出現(xiàn)了以下問題:

解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題

問題具體表現(xiàn)為:

1.幾個負(fù)樣本的數(shù)據(jù)點位置倒錯

2.X軸刻度變成了亂七八糟一團(tuán)鬼東西

最終解決辦法

造成上述情況的原因其實是由于輸入matplotlib.plot()函數(shù)的數(shù)據(jù)x_data和y_data從CSV文件中直接導(dǎo)入后格式為string,因此才會導(dǎo)致所有數(shù)據(jù)點的x坐標(biāo)都被直接刻在了x軸上,且由于坐標(biāo)數(shù)據(jù)格式錯誤,部分點也就表現(xiàn)為“亂點”。解決辦法就是導(dǎo)入x,y數(shù)據(jù)后先將其轉(zhuǎn)化為float型數(shù)據(jù),然后輸入plot()函數(shù),問題即解決。

解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題

補充知識:matplotlib如何在繪制時間序列時跳過無數(shù)據(jù)的區(qū)間

其實官方文檔里就提供了方法,這里簡單的翻譯并記錄一下.

11.1.9 Skip dates where there is no dataWhen plotting time series, e.g., financial time series, one often wants to leave out days on which there is no data, e.g., weekends.By passing in dates on the x-xaxis, you get large horizontal gaps on periods when there is not data.

The solution is to pass in some proxy x-data, e.g., evenly sampled indices, and then use a custom formatter to format these as dates.The example below shows how to use an ‘index formatter’ to achieve the desired plot:

解決方案是通過傳遞x軸數(shù)據(jù)的代理,比如下標(biāo),

然后通過自定義的’formatter’去取到相對應(yīng)的時間信息

manual內(nèi)示例代碼:

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.mlab as mlabimport matplotlib.ticker as ticker#讀數(shù)據(jù)r = mlab.csv2rec(’../data/aapl.csv’)r.sort()r = r[-30:] # get the last 30 daysN = len(r)ind = np.arange(N) # the evenly spaced plot indicesdef format_date(x, pos=None): #保證下標(biāo)不越界,很重要,越界會導(dǎo)致最終plot坐標(biāo)軸label無顯示 thisind = np.clip(int(x+0.5), 0, N-1) return r.date[thisind].strftime(’%Y-%m-%d’)fig = plt.figure()ax = fig.add_subplot(1,1,1)ax.plot(ind, r.adj_close, ’o-’)ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))fig.autofmt_xdate()plt.show()

示例:

同樣一段數(shù)據(jù)上為原始,下為去掉無數(shù)據(jù)間隔區(qū)間

解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題

import pandas as PDimport numpy as NPimport matplotlib.pyplot as PLTimport matplotlib.ticker as MTKfile = r’vix_series.csv’df = PD.read_csv(file, parse_dates=[0, 2])#用下標(biāo)代理原始時間戳數(shù)據(jù)idx_pxy = NP.arange(df.shape[0])#下標(biāo)-時間轉(zhuǎn)換funcdef x_fmt_func(x, pos=None): idx = NP.clip(int(x+0.5), 0, df.shape[0]-1) return df[’datetime’].iat[idx]#繪圖流程def decorateAx(ax, xs, ys, x_func): ax.plot(xs, ys, color='green', linewidth=1, linestyle='-') ax.plot(ax.get_xlim(), [0,0], color='blue', linewidth=0.5, linestyle='--') if x_func: #set數(shù)據(jù)代理func ax.xaxis.set_major_formatter(MTK.FuncFormatter(x_func)) ax.grid(True) returnfig = PLT.figure()ax1 = fig.add_subplot(2,1,1)ax2 = fig.add_subplot(2,1,2)decorateAx(ax1, df[’datetime’], df[’vix_all’], None)decorateAx(ax2, idx_pxy, df[’vix_all’], x_fmt_func)#優(yōu)化label顯示,非必須fig.autofmt_xdate()PLT.show()

很多時候亂翻google還不如好好通讀官方manual…

以上這篇解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 午夜免费剧场 | 国产九九热| 深夜福利视频在线观看 | 激情五月婷婷综合 | 久久精品视频一区二区 | av手机在线免费观看 | 秋霞一区二区 | 亚洲欧美成人 | 理论片中文字幕 | 国产一级18片视频 | 日韩在线视频观看 | 日韩精品影视 | 成人免费黄色大片 | www久久| 蜜臀久久99精品久久久久宅男 | 日本不卡视频在线观看 | 中文字幕丰满人伦在线 | 欧美午夜精品久久久久免费视 | 成人永久免费视频 | 超碰精品在线 | 久久精品一区二区国产 | 国产高清成人 | 老女人性生活视频 | 色哟哟一区二区三区 | 亚欧洲精品在线视频免费观看 | 日韩一区二区在线播放 | 青青青视频在线 | 一区二区高清视频 | 狠狠操狠狠爱 | 免费在线成人网 | 亚洲人在线观看 | 日韩精品一区二区三区免费视频 | 中文字幕在线视频观看 | 91日本视频 | www.欧美精品 | www.亚洲天堂 | 欧美在线网站 | 亚洲精品久久久久 | 天堂av影院 | 精品粉嫩小bbwbbwbbw | 久久久久久久久久国产 |