2017-07-14 56 views
3

在常規matplotlib中,您可以指定各種標記樣式。但是,如果我導入seaborn,「+」和「x」樣式停止工作並導致圖塊不顯示 - 其他標記類型,例如'o','v'和'*'起作用。Python:更改Seaborn中的標記類型

簡單的例子:

import matplotlib.pyplot as plt 
import seaborn as sns 

x_cross = [768] 
y_cross = [1.028e8] 
plt.plot(x_cross, y_cross, 'ok') 

plt.gca().set_xlim([10, 1e4]) 
plt.gca().set_ylim([1, 1e18]) 
plt.xscale('log') 
plt.yscale('log') 

plt.show() 

產生以下:Simple Seaborn Plot

更改 'OK' 第6行至然而 '+ K',不再示出了繪製的點。如果我不導入seaborn它的作品,因爲它應該:Regular Plot With Cross Marker

可能有人請賜教,我怎麼更改標記樣式的十字型使用seaborn什麼時候?

回答

4

這樣做的原因行爲是seaborn設置標記邊緣寬度爲零。 (見source)。

正如指出的seaborn known issues

的matplotlib標記樣式的工作方式是,藝術線條標記(例如"+")或facecolor設置爲"none"標記的不幸後果將是不可見的時候默認seaborn風格是有效的。這可以通過在函數調用中使用不同的markeredgewidth(別名爲mew)或在rcParams中全局使用。

This issue告訴我們關於它以及this one

在這種情況下,該解決方案是將markeredgewidth設置的東西大於零,

  • 使用rcParams(導入seaborn後):使用markeredgewidthmew關鍵字

    plt.rcParams["lines.markeredgewidth"] = 1 
    
  • 論點

    plt.plot(..., mew=1) 
    

不過,正如@mwaskom在評論中指出的那樣,實際上還有更多。在this issue中有人認爲,標記應該分爲兩類,散裝樣式標記和線條標記。這在matplotlib 2.0版本中已經部分完成,您可以使用marker="P"獲得「plus」作爲標記,即使使用markeredgewidth=0,該標記也將可見。

plt.plot(x_cross, y_cross, 'kP') 

enter image description here

+0

啊;我想我應該總是在將來首先檢查「已知問題」。感謝您擴展以前的答案。 –

+1

你鏈接到的兩個問題實際上是一個類似但獨立的問題,它是matplotlib 1.4.2中的一個錯誤。最相關的問題是在matplotlib github這裏:https://github.com/matplotlib/matplotlib/issues/4679。這在matplotlib 2中通過添加線性標記的「填充」版本來處理,例如,不管線寬設置如何,「P」會畫出一個加號。 – mwaskom

2

它非常想成爲一個bug。然而,你可以通過mew關鍵字設置標記邊緣線寬度得到你想要的東西:

import matplotlib.pyplot as plt 
import seaborn as sns 

x_cross = [768] 
y_cross = [1.028e8] 

# set marker edge line width to 0.5 
plt.plot(x_cross, y_cross, '+k', mew=.5) 

plt.gca().set_xlim([10, 1e4]) 
plt.gca().set_ylim([1, 1e18]) 
plt.xscale('log') 
plt.yscale('log') 

plt.show() 

enter image description here

+0

這是完美的。非常感謝。 –