I have a bunch of data cotaining coordinate intervals within one large region, which i want to plot and then create a density plot showing where in the region there are more interval lines than others.
As a very basic example i've just plotted some horizontal lines for a given interval. I cant really find any good examples of how to create a better plot of intervals. I've looked into seaborn, but im not entirely sure about it. So here i've just created a basic example of what i am trying to do.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(1, 30,100)
x2 = np.linspace(10,40,100)
x3 = np.linspace(2,50,100)
x4 = np.linspace(40,60,100)
x5 = np.linspace(30,78,100)
x6 = np.linspace(82,99,100)
x7 = np.linspace(66,85,100)
x = [x1,x2,x3,x4,x5,x6,x7]
y = np.linspace(1,len(x),len(x))
fig, ax = plt.subplots()
for i in range(len(x)):
ax.hlines(y[i], xmin=x[i][0], xmax=x[i][-1], linewidth=1)
plt.xlim(-5,105)
plt.show()
And then I would like to create a density plot of the number of intervals overlapping. Could anyone have any suggestions on how to proceed with this?
Thans for your help and suggestions
This seems to do what you want:
def count(xi):
samples = np.linspace(0, 100, 101)
return (xi[0] < samples) & (samples <= xi[-1])
is_in_range = np.apply_along_axis(count, arr=x, axis=1)
density = np.sum(is_in_range, axis=0)
The general idea is to make some output linspace
, then check to see if those coordinates are in the ranges in the array x
— that's what the function count
does. Then apply_along_axis
runs this function on every row (i.e. every 1D array) in your array x
.
Here's what I get when I plot density
:
You might want to adjust the <=
and <
signs in the count
function to handle the edges as you like.
If your actual data have a different format, or if there are multiple intervals in one array, you will need to adjust this.