在matplotlib中绘制概率分布的积分 [英] Plotting the integral of a probability distribution in matplotlib

查看:119
本文介绍了在matplotlib中绘制概率分布的积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制概率分布的积分,类似于此图像:

I'm trying to plot the integral of probability distribution, similar to this image:

请注意Y轴如何开始朝顶部逐渐变细.

Notice how the Y-axis starts becoming more granular towards the top.

我已经有了要绘制的确切百分位数,以及x轴的相应值.

I already have the exact percentiles that I want to plot, and the corresponding values for the x-axis.

到目前为止,我的代码是:

My code so far is:

import matplotlib.pylab as plt
import numpy as np
import scipy as sp
from scipy import stats

x = np.array([0.0000025,0.000005,0.00001,0.00002,0.00003,0.00004,0.00005,0.00006,0.00007,0.00008,0.00009,0.0001,0.0002,0.00025,0.00035,0.0005,0.001,0.002,0.005,0.01,1])
y = np.array([0,0,0,0,0,0,0,0,46.29821447,49.49781571,49.83072758,50.89081787,98.49113721,98.5522082,99.29547499,99.91765345,99.93779431,99.95351796,99.98066963,99.99294867,100])
my_xticks = ['<2.5 uS', '<5 uS', '<10 uS', '<20 uS', '<30 uS', '<40 uS', '<50 uS', '<60 uS', '<70 uS', '<80 uS', '<90 uS', '<100 uS', '<200 uS', '<250 uS', '<350 uS', '<500 uS', '<1 mS', '<2 uS', '<5 mS', '<10 mS', '<1 S']
plt.xticks(x, my_xticks)
plt.yticks(np.arange(y.min() - 20, y.max() + 1, 10))
plt.xticks(np.arange(x.min() + 0.035, x.max(), 0.08))
plt.plot(x, y)
plt.grid(axis='y')
plt.show()

哪个输出:

更新

因此,我设法精确地分割了多个图10,同时为每个图分配了不同的y轴限制,以便将数据点分散开以提高可读性.所有这些都是硬编码的,这提出了自己的挑战,因为我将以不同的向量在值上非常接近的方式提供动态数据.我可以设计函数来分析值彼此之间的接近度,基于它可以绘制出x个图并分配限制,但这在必须绘制多个图的工作和资源上过多.这是我到目前为止所拥有的,

So I managed to split multiple plots 10 to be precise while assigning different y-axis limitations to each in order to spread my data points apart for readability. This is all hard coded which presents its own challenges because I will feed dynamic data with different vectors being really close in values. I could engineer function to analyze the proximity of the values to each other, based on that have it plot x amount of plots and assign limitations, but this is excessive on work and resources having to plot multiple plots. Here is what I have so far,

x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21])
y = np.array([0,0,0,0,0,0,0,0,46.29821447,49.49781571,49.83072758,50.89081787,98.49113721,98.5522082,99.29547499,99.91765345,99.93779431,99.95351796,99.98066963,99.99294867,100])
my_xticks = ['<2.5 uS', '<5 uS', '<10 uS', '<20 uS', '<30 uS', '<40 uS', '<50 uS', '<60 uS', '<70 uS', '<80 uS', '<90 uS', '<100 uS', '<200 uS', '<250 uS', '<350 uS', '<500 uS', '<1 mS', '<2 uS', '<5 mS', '<10 mS', '<1 S']
f,(ax,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9,ax10) = plt.subplots(10,1,sharex=True)

majorFormatter = FormatStrFormatter('%.7f')
plt.subplots_adjust(hspace=0)
plt.xticks(x, my_xticks)
ax.grid(axis='y')
ax2.grid(axis='y')
ax3.grid(axis='y')
ax4.grid(axis='y')
ax5.grid(axis='y')
ax6.grid(axis='y')
ax7.grid(axis='y')
ax8.grid(axis='y')
ax9.grid(axis='y')
ax10.grid(axis='y')

ax.plot(x,y, '-r')
ax.plot(x,y, '.')
ax2.plot(x,y, '.')
ax2.plot(x,y, '-r')
ax3.plot(x,y, '.')
ax3.plot(x,y, '-r')
ax4.plot(x,y, '.')
ax4.plot(x,y, '-r')
ax5.plot(x,y, '.')
ax5.plot(x,y, '-r')
ax6.plot(x,y, '.')
ax6.plot(x,y, '-r')
ax7.plot(x,y, '.')
ax7.plot(x,y, '-r')
ax8.plot(x,y, '.')
ax8.plot(x,y, '-r')
ax9.plot(x,y, '.')
ax9.plot(x,y, '-r')
ax10.plot(x,y, '.')
ax10.plot(x,y, '-r')

ax.set_yticks(y)
ax2.set_yticks(y)
ax3.set_yticks(y)
ax4.set_yticks(y)
ax5.set_yticks(y)
ax6.set_yticks(y)
ax7.set_yticks(y)
ax8.set_yticks(y)
ax9.set_yticks(y)
ax10.set_yticks(y)

ax.set_ylim(99.95,100)
ax2.set_ylim(99.8,99.95)
ax3.set_ylim(99.5,99.8)
ax4.set_ylim(99,99.5)
ax5.set_ylim(98.5,99)
ax6.set_ylim(93,98.5)
ax7.set_ylim(90,93)
ax8.set_ylim(86,90)
ax9.set_ylim(70,86)
ax10.set_ylim(0,70)

ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax4.spines['top'].set_visible(False)
ax4.spines['bottom'].set_visible(False)
ax5.spines['top'].set_visible(False)
ax5.spines['bottom'].set_visible(False)
ax6.spines['top'].set_visible(False)
ax6.spines['bottom'].set_visible(False)
ax7.spines['top'].set_visible(False)
ax7.spines['bottom'].set_visible(False)
ax8.spines['top'].set_visible(False)
ax8.spines['bottom'].set_visible(False)
ax9.spines['top'].set_visible(False)
ax9.spines['bottom'].set_visible(False)
ax10.spines['top'].set_visible(False)

ax.yaxis.set_major_formatter(majorFormatter)
ax2.yaxis.set_major_formatter(majorFormatter)
ax3.yaxis.set_major_formatter(majorFormatter)
ax4.yaxis.set_major_formatter(majorFormatter)
ax5.yaxis.set_major_formatter(majorFormatter)
ax6.yaxis.set_major_formatter(majorFormatter)
ax7.yaxis.set_major_formatter(majorFormatter)
ax8.yaxis.set_major_formatter(majorFormatter)
ax9.yaxis.set_major_formatter(majorFormatter)
ax10.yaxis.set_major_formatter(majorFormatter)

plt.show()

推荐答案

y x = 0.00006的0增加到 x = 0.01,然后仅在 x = 0.01和 x = 1之间再增加〜0.007.线基本上呈L形笔直向上射击到被轴遮盖的图的左上角和上边界.

y increases from 0 at x = 0.00006, to 99.99 at x = 0.01, then only increases by a further ~0.007 between x = 0.01 and x = 1. The line basically shoots straight up in an L-shape that is so close to the left-hand and upper borders of the plot that it is obscured by the axes.

如果将轴限制设置为稍微宽一些:

If you set your axis limits slightly wider:

plt.xlim(-0.1, 1.1)
plt.ylim(-10, 110)

然后,您可以轻松查看正在发生的事情:

then you can easily see what's happening:

正如@snorthway所说,以对数-对数比例绘制此数据可能更明智(如您显示的示例图像所示):

As @snorthway commented, it might be more sensible to plot this data on log-log scales (as in the example image you showed):

plt.loglog(x, y)

但是,随后您会遇到 y 值包含0的问题.由于log(0)= -infinity,因此对于 x < 0.00007.

However you will then run into the issue that your y-values contain 0s. Since log(0) = -infinity, the line becomes vertical for x < 0.00007.

这篇关于在matplotlib中绘制概率分布的积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆