Search code examples
pythonfunctionplotcurve

Mathplotlib draw delta on curve + bars under function


I have this code:

import matplotlib.pyplot as plt
import numpy as np

gamma = 0.5

p = np.linspace(1/253, 253/253, 253)
y = np.power(p, gamma)

plt.plot(p, y)
plt.xlabel('p')
plt.ylabel('\u03C6(p)')
plt.title(' ')
plt.show()

which gives me this figure: https://ibb.co/M7B1PD1

And I have this code:

p = np.linspace(1/253, 253/253, 253)

b = 0.5
phi_p = np.power((b * p), (b-1))

plt.plot(p, phi_p)
plt.xlabel('p')
plt.ylabel('\u03A6(p)')
plt.title(' ')
plt.show()

which gives me this figure: https://ibb.co/5xRR1k6

Now I want this for the first figure:https://ibb.co/nzdpfNc and this for the second one: https://ibb.co/yYCp7HD

So in the first picture the delta of the curve should be shown and in the second picture a bar for each i in (0,253) should be drawn under the curve.

Is there I way to do this with mathplotlib? I had the idea to draw a histogram under the curve but I don't solve this well..


Solution

  • To plot the delta under the curve, just pair up your data points one at a time and then plot it. For instance

    x_data=[1,4,6]
    y_data=[2,3,7]
    delta_to_plot_x=[1,4,4,6,6]
    delta_to_plot_y=[2,2,3,3,7]
    

    and this function should work at generating the right values.

    def make_delta_for_plot(x,y):
        new_x=[val for val in x for _ in (0, 1)]
        new_y=[val for val in y for _ in (0, 1)]
        new_x.pop(0)
        new_y.pop(-1)
        return new_x,new_y
    

    For the second plot, seaborn.distplot can plot distributions with histograms at the same time (and it uses matplotlib so it will work fine with the rest of your code). There are plenty of examples in the link