我正在使用 Python 2.7 并使用 scipy.stats.probplot 函数创建了一个概率图。我想更改图形的元素,例如标记的颜色/形状/大小以及最适合趋势线的颜色/宽度。 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](/image/7J8jl.png)
最佳答案
关键是与matplotlib的结合。
您可以使用 ax.get_lines()
从 line object 访问 axes 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](/image/6VUHg.png)
文本 (
r^2=0.9616
) 可以通过轴上更通用的 get_children
访问:ax.get_children()[2].set_fontsize(22.0)
如果没有详细了解这些项目的索引,您可以尝试:
print ax.get_children()
这给了你:
[<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>]
关于python-2.7 - 在 Python Probplot 中更改标记样式/颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463189/