python-2.7 - 在 Python Probplot 中更改标记样式/颜色

标签 python-2.7 matplotlib scipy

我正在使用 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

最佳答案

关键是与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

文本 ( 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/

相关文章:

python - 在 scipy 中使用大 F 阶矩阵进行 dgemm 段错误

python - 从 NumPy 二维数组中删除重复的列和行

python - SciPy 1D 高斯拟合

python - 用 matplotlib.pyplot 绘制 vlines

python - 将 pandas DataFrame 与日期时间进行比较时出错

python - “模块”对象没有属性 '_strptime',有多个线程 Python

python-2.7 - 无法将数据插入本地 Google App Engine 数据存储区

python - 如何将彩色图图像反转为标量值?

python - 在 seaborn 散点图中对分类 x 轴进行排序

python-2.7 - Pathos multiprocessing 无法调用类中的任何包和函数