Tra*_*isJ 3 matplotlib scipy python-2.7
我正在使用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()
Run Code Online (Sandbox Code Playgroud)
我尝试使用ax.get_xydata()和fig.get_xydata()来获取xy数据并创建自己的散点图.但是这两个都失败了,因为两个对象都没有将get_xydata()作为函数.我的代码当前生成的数字是:
关键是与matplotlib的结合.
您可以访问行对象从对象轴使用ax.get_lines().然后,可以相应地改变属性.
您可能必须弄清楚哪个索引与标记有关,哪些与线路有关.在下面的示例中,标记首先出现,因此:
ax.get_lines()[0].set_marker('p')
Run Code Online (Sandbox Code Playgroud)
趋势线是第二个:
ax.get_lines()[1].set_linewidth(12.0)
Run Code Online (Sandbox Code Playgroud)
以下示例基于probplot文档:
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()
Run Code Online (Sandbox Code Playgroud)
这个创建的图看起来很丑,但演示了功能:
r^2=0.9616可以通过get_children轴中更一般的方式访问text():
ax.get_children()[2].set_fontsize(22.0)
Run Code Online (Sandbox Code Playgroud)
如果没有详细了解这些项目的索引,您可以尝试:
print ax.get_children()
Run Code Online (Sandbox Code Playgroud)
这给你:
[<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>]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |