Home > OS >  Color overlapped lines with plt.plot (Matplotlib)
Color overlapped lines with plt.plot (Matplotlib)

Time:01-01

How can I configure plt.plot such that overlapped lines will have darker colors?
For example, I would like to use plt.plot to display the samples in such a way that the density that can be seen in the upper plot will be clear in the lower plot.
From the lower plot it's hard to understand where most of the samples are located

plot example

Here is the code I used in order to generate the example:

import numpy as np
import matplotlib.pyplot as plt

time = 100
n_samples = 7000
x = np.linspace(0, time, n_samples)
r1 = np.random.normal(0, 1, x.size)
r2 = np.random.uniform(-6, 6, x.size)
data = np.dstack((r1, r2)).flatten()

fig, axs = plt.subplots(2, 1, figsize=(9, 6))
axs[0].scatter(np.arange(len(data)), data, alpha=0.1)
axs[1].plot(np.arange(len(data)), data, alpha=0.2)
plt.show()

CodePudding user response:

I found this code:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
 
data = np.random.normal(10,3,100) # Generate Data
density = gaussian_kde(data)
 
x_vals = np.linspace(0,20,200) # Specifying the limits of our data
density.covariance_factor = lambda : .5 #Smoothing parameter
 
density._compute_covariance()
plt.plot(x_vals,density(x_vals))
plt.show()

From: drawing individual segments

  • Related