Example of Python Code Demonstrating CDF Plotting in Matplotlib

Solution 1:
I enjoy extracting my colors directly from a colormap using the following code. In this case, colormap represents the name of the colormap (refer to https://matplotlib.org/examples/color/colormaps_reference.html for a list), the total number of desired colors is specified by , and represents the specific color index.
Solution 3:
The simplest approach is to assign a distinct color to the last curve. For instance, Matplotlib version 1.5 or earlier offers a color cycle consisting of 7 different colors, whereas Matplotlib 2.0 includes a cycle of 10 distinct colors.


Solution:

I’m uncertain about the capabilities of

step()

in handling this task; it essentially consists of only a few lines of code that are built around

plt.plot()

.

Instead, you have the option to utilize both

vlines()

and

hlines()

. The method of slicing differs depending on the desired configuration of the steps. For instance, the

where

argument for

step()

can be specified in various ways. However, here is an approximation of the example you provided in your question.

import numpy as np
import matplotlib.pyplot as plt
data = np.arange(0, 7)
y = np.array([.07, .21, .42, .68, 1.])
yn = np.insert(y, 0, 0)
fig, ax = plt.subplots()
ax.set_facecolor('white')
# https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.hlines.html
ax.hlines(y=yn, xmin=data[:-1], xmax=data[1:],
          color='red', zorder=1)
# https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.vlines.html
ax.vlines(x=data[1:-1], ymin=yn[:-1], ymax=yn[1:], color='red',
          linestyle='dashed', zorder=1)
ax.scatter(data[1:-1], y, color='red', s=18, zorder=2)
ax.scatter(data[1:-1], yn[:-1], color='white', s=18, zorder=2,
           edgecolor='red')
ax.grid(False)
ax.set_xlim(data[0], data[-1])
ax.set_ylim([-0.01, 1.01])

The

zorder

function ensures that the scatter points are placed on top of the lines.

The

y

you made doesn’t match the image you provided, but this example attempts to replicate the image.

Frequently Asked Questions