2016-05-26 87 views
2

我正在使用Python 2.7並使用scipy.stats.probplot函數創建了概率圖。我想更改圖形的元素,例如標記的顏色/形狀/大小以及最適合趨勢線的顏色/寬度。 probplot的文檔似乎沒有任何更改這些項目的選項。在Python中更改標記樣式/顏色Probplot

這裏是我的代碼(相關部分):

#data is a list of y-values sampled from a lognormal distribution 
d = getattr(stats, 'lognorm') 
param = d.fit(data) 
fig = plt.figure(figsize=[6, 6], dpi=100) 
ax = fig.add_subplot(111) 
fig = stats.probplot(data, dist='lognorm', sparams=param, plot=plt, fit=False) 
#These next 3 lines just demonstrate that some plot features 
#can be changed independent of the probplot function. 
ax.set_title("") 
ax.set_xlabel("Quantiles", fontsize=20, fontweight='bold') 
ax.set_ylabel("Ordered Values", fontsize=20, fontweight='bold') 
plt.show() 

我試圖抓住XY-數據和創建具有ax.get_xydata我自己的散點圖()和fig.get_xydata()。但是,這兩個都失敗了,因爲這兩個對象都沒有get_xydata()作爲函數。我的代碼目前生成的數字是:

the plot generated by probplot

回答

3

的關鍵是與matplotlib的組合。

您可以使用ax.get_lines()axes object訪問line object。然後,屬性可以相應地改變。

您可能必須弄清楚哪些索引與標記相關,哪些與行相關。在下面的例子中,標記是第一位的,因此:

ax.get_lines()[0].set_marker('p') 

和趨勢線是第二:

ax.get_lines()[1].set_linewidth(12.0) 

下面的例子是基於probplot documentation

import numpy as np 
from scipy import stats 
import matplotlib.pyplot as plt 

nsample = 100 
np.random.seed(7654321) 

fig = plt.figure() 
ax = fig.add_subplot(111) 
x = stats.t.rvs(3, size=nsample) 
res = stats.probplot(x, plot=plt) 

ax.get_lines()[0].set_marker('p') 
ax.get_lines()[0].set_markerfacecolor('r') 
ax.get_lines()[0].set_markersize(12.0) 

ax.get_lines()[1].set_linewidth(12.0) 

plt.show() 

的情節這創造看起來醜陋,但演示功能:

updated markers


文本(r^2=0.9616)可以通過更廣泛的get_children從軸訪問:

print ax.get_children() 

ax.get_children()[2].set_fontsize(22.0) 

沒有索引這些項目的詳細知識,您可以嘗試

它給你:

[<matplotlib.lines.Line2D object at 0x33f4350>, <matplotlib.lines.Line2D object at 0x33f4410>, 
<matplotlib.text.Text object at 0x33f4bd0>, <matplotlib.spines.Spine object at 0x2f2ead0>, 
<matplotlib.spines.Spine object at 0x2f2e8d0>, <matplotlib.spines.Spine object at 0x2f2e9d0>, 
<matplotlib.spines.Spine object at 0x2f2e7d0>, <matplotlib.axis.XAxis object at 0x2f2eb90>, 
<matplotlib.axis.YAxis object at 0x2f37690>, <matplotlib.text.Text object at 0x2f45290>, 
<matplotlib.text.Text object at 0x2f45310>, <matplotlib.text.Text object at 0x2f45390>, 
<matplotlib.patches.Rectangle object at 0x2f453d0>] 
+0

我假設(將很快測試)get_lines()[2]將是r^2文本嗎?非常感謝。 – TravisJ

+0

@TravisJ - 不完全,它不是一個線對象,而是一個文本對象。看我的編輯。 – Schorsch

+0

我希望我可以upvote兩次的雙重答案。這非常有幫助。我假定每個孩子都是情節的某個方面(可能是背景,勾號標籤,情節內容 - 散點,線條,文本等)。 – TravisJ