2016-04-13 78 views
1

我正在繪製使用pandas數據框的散點圖。這工作正常,但我想使用seaborn主題和特殊功能。當我繪製調用seaborn的相同數據點時,y軸幾乎不可見。 X軸值範圍從5000-15000,而Y軸值在[-6:6]*10^-7seaborn對熊貓的y軸縮放

如果我將y軸值乘以10^6,它們將正確顯示,但使用seaborn繪製的實際值在seaborn生成的繪圖中保持不可見/無法區分。

我怎麼能seaborn,使y軸值自動生成的打印比例?

還有一些行甚至包含NaN,而不是在這種情況下,如何在繪圖時忽略這一點,缺少手動除去包含NaN的行。

下面是我用來繪製的代碼。

import pandas as pd 
import seaborn as sns 
import matplotlib.pyplot as plt 


df = pd.read_csv("datascale.csv") 
subdf = df.loc[(df.types == "easy") & (df.weight > 1300), ] 

subdf = subdf.iloc[1:61, ] 
subdf.drop(subdf.index[[25]], inplace=True) #row containing NaN 

subdf.plot(x='length', y='speed', style='s') #scales y-axis correctly 

sns.lmplot("length", "speed", data=subdf, fit_reg=True, lowess=True) #doesn't scale y-axis properly 

# multiplying by 10^6 displays the plot correctly, in matplotlib 
plt.scatter(subdf['length'], 10**6*subdf['speed']) 
+0

目前還不清楚你的意思是「y軸保持不可見」,並且它不可能運行你的代碼... – mwaskom

回答

2

奇怪的是,seaborn不能正確縮放的軸。儘管如此,你可以糾正這種行爲。首先,到劇情的軸對象的引用:

lm = sns.lmplot("length", "speed", data=subdf, fit_reg=True) 

之後,你可以手動設置Y軸限制:

lm.axes[0,0].set_ylim(min(subdf.speed), max(subdf.speed)) 

結果應該是這個樣子:

enter image description here

例Jupyter筆記本here

+0

@mwaskom這個回答解決了我所面臨的問題,價值太小,似乎變得幾乎看不見。 – Frash

+0

Markus如何顯示修正了軸限制的最終繪圖?我正在使用Spyder,並且提示不會更新或繪製新的繪圖。我是新來的蟒蛇業務,不知道如何顯示更新的情節。 – Frash

+0

'''sns.plt.show()'''應該顯示最後一個圖。 –

0

Seaborn和matplotlib應打印時忽略NaN值。你應該能夠保持原樣。

至於在y縮放:有可能是seaborn的錯誤。

最基本的解決方法仍然是在繪製之前對數據進行縮放。 在繪製和繪製微米速度之前,將其縮放到數據幀中的微速。

subdf['microspeed']=subdf['speed']*10**6 

或轉換密謀之前登錄Y,即

import math 
df = pd.DataFrame({'speed':[1, 100, 10**-6]}) 
df['logspeed'] = df['speed'].map(lambda x: math.log(x,10)) 

然後情節logspeed,而不是速度。

另一種方法是使用seaborn regplot instead

Matplot LIB正確尺度,對我圖如下:

plt.plot(subdf['length'], subdf['speed'], 'o') 
+0

但是這樣會丟失信息,我得到的是日誌值而不是正常值,這會導致比例/相對值。另外lowess適合只是一個後想,主要是我試圖得到一個散點圖。 – Frash

+0

如何通過'matlablotlib'將'pandas'接口複製到'seaborn'中? – Frash

+0

編輯答案包括matplotlib解決方案。 – SpeedCoder5