欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年05月16日 16:17:42   作者:container_body  
這篇文章主要介紹了解決Python Matplotlib繪圖數(shù)據(jù)點位置錯亂問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

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

2.X軸刻度變成了亂七八糟一團鬼東西

最終解決辦法

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

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

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

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:

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

然后通過自定義的'formatter'去取到相對應的時間信息

manual內示例代碼:

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):
 #保證下標不越界,很重要,越界會導致最終plot坐標軸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ū)間

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])
#用下標代理原始時間戳數(shù)據(jù)
idx_pxy = NP.arange(df.shape[0])
#下標-時間轉換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, 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)
 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()

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

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

相關文章

  • python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 基于BCEWithLogitsLoss樣本不均衡的處理方案

    基于BCEWithLogitsLoss樣本不均衡的處理方案

    這篇文章主要介紹了BCEWithLogitsLoss樣本不均衡的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • pyspark給dataframe增加新的一列的實現(xiàn)示例

    pyspark給dataframe增加新的一列的實現(xiàn)示例

    這篇文章主要介紹了pyspark給dataframe增加新的一列的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • python爬取淘寶商品銷量信息

    python爬取淘寶商品銷量信息

    這篇文章主要為大家詳細介紹了python爬取淘寶商品的銷量信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • window下eclipse安裝python插件教程

    window下eclipse安裝python插件教程

    這篇文章主要為大家詳細介紹了window下eclipse安裝python插件教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • tensorflow 分類損失函數(shù)使用小記

    tensorflow 分類損失函數(shù)使用小記

    這篇文章主要介紹了tensorflow 分類損失函數(shù)使用小記,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • python PyTorch預訓練示例

    python PyTorch預訓練示例

    這篇文章主要介紹了python PyTorch預訓練示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • python如何生成各種隨機分布圖

    python如何生成各種隨機分布圖

    這篇文章主要為大家詳細介紹了python如何生成各種隨機分布圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Python如何利用pandas讀取csv數(shù)據(jù)并繪圖

    Python如何利用pandas讀取csv數(shù)據(jù)并繪圖

    這篇文章主要介紹了Python如何利用pandas讀取csv數(shù)據(jù)并繪圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python 面向對象之類class和對象基本用法示例

    Python 面向對象之類class和對象基本用法示例

    這篇文章主要介紹了Python 面向對象之類class和對象基本用法,結合實例形式詳細分析了Python面向對象程序設計中類class和對象基本概念、原理、使用方法與操作注意事項,需要的朋友可以參考下
    2020-02-02

最新評論