Yasko
Yasko

Reputation: 83

Plotting the integral of a probability distribution in matplotlib

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

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

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()

Which outputs this:

output

Update

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()

graph

Upvotes: 2

Views: 484

Answers (1)

ali_m
ali_m

Reputation: 74154

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:

enter image description here

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)

enter image description here

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.

Upvotes: 3

Related Questions