溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法

發(fā)布時(shí)間:2020-07-23 17:35:29 來(lái)源:億速云 閱讀:271 作者:小豬 欄目:開(kāi)發(fā)技術(shù)

小編這次要給大家分享的是Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

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

Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法

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

1.幾個(gè)負(fù)樣本的數(shù)據(jù)點(diǎn)位置倒錯(cuò)

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

最終解決辦法

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

Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法

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

其實(shí)官方文檔里就提供了方法,這里簡(jiǎn)單的翻譯并記錄一下.

11.1.9 Skip dates where there is no data
When 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:

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

然后通過(guò)自定義的'formatter'去取到相對(duì)應(yīng)的時(shí)間信息

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

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.ticker as ticker

#讀數(shù)據(jù)
r = mlab.csv2rec('../data/aapl.csv')
r.sort()
r = r[-30:] # get the last 30 days
N = len(r)
ind = np.arange(N) # the evenly spaced plot indices
def format_date(x, pos=None):
 #保證下標(biāo)不越界,很重要,越界會(huì)導(dǎo)致最終plot坐標(biāo)軸label無(wú)顯示
 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ù)上為原始,下為去掉無(wú)數(shù)據(jù)間隔區(qū)間

Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法

import pandas as PD
import numpy as NP
import matplotlib.pyplot as PLT
import matplotlib.ticker as MTK

file = r'vix_series.csv'
df = PD.read_csv(file, parse_dates=[0, 2])
#用下標(biāo)代理原始時(shí)間戳數(shù)據(jù)
idx_pxy = NP.arange(df.shape[0])
#下標(biāo)-時(shí)間轉(zhuǎn)換func
def 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, line)
 ax.plot(ax.get_xlim(), [0,0], color="blue", 
   linewidth=0.5, line)
 if x_func:
  #set數(shù)據(jù)代理func
  ax.xaxis.set_major_formatter(MTK.FuncFormatter(x_func))
 ax.grid(True)
 return

fig = 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()

看完這篇關(guān)于Python Matplotlib繪圖數(shù)據(jù)點(diǎn)位置錯(cuò)亂問(wèn)題解決方法的文章,如果覺(jué)得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI