Make a joyplot-like plot of R in python

1. What is joyplot?

joyplot is a plot like this that was talked about on twitter a while ago.

This seems to be easy to plot in R's ggjoy package. There didn't seem to be anything similar in python.

By the way, the reason why it is called joy plot is that the cover of the album "Unknown Pleasures" of the band Joy Division looks like the one below, so it seems to be called joy plot. pulsar_detail.jpg

So, when using this joyplot, it seems polite to plot while listening to this. (Is a lie)

2. Environment

3. Data generation

Looking at this plot, I thought that it would be useful for Bayesian update and spectrogram plotting, so this time I will try spectrogram plotting for the time being. Use the example of the scipy spectrogram as it is.

from scipy import signal
import matplotlib.pyplot as plt
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*time)
carrier = amp * np.sin(2*np.pi*3e3*time + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
data = carrier + noise

f, t, Sxx = signal.spectrogram(data, fs, nperseg=1000, noverlap=900)

4. Plot

I refer to here on stackoverflow.


y = Sxx.T

#Since there are many, slice it.
y_subset_num = 10
x_subset_num = 20
y = y[0:y_subset_num, 0:x_subset_num]
ny, nx = y.shape 
x = f[0:x_subset_num]

y_delta = (y.max() - y.min()) / ny #This y=Plot by shifting by delta.
yticks = [] #For saving the position of yticks

fig = plt.figure()
ax = fig.add_subplot(111)

for iy in np.arange(ny):
    offset = (ny-iy) * y_delta #Plot from top to bottom
    y_value = y[iy] + offset #Shift by offset.
    ax.plot(x, y_value, color='w', lw=1, zorder=iy)
    ax.fill_between(x=x, y1=y_value, y2=offset, alpha=0.5, lw=0, zorder=iy, color="red")
    # y_Plot the value line and paint under it.
    yticks.append(offset)

#The top of the plot is younger, so reverse the order.
yticks = yticks[::-1]
ytickslabel = np.arange(len(yticks))[::-1]

ax.set_yticks(yticks)
ax.set_yticklabels(ytickslabel)

ax.set_xlabel("Frequency [Hz]")
plt.show()

figure_1.png

I feel like this. In order to make it look beautiful, you need to make some adjustments. You need to change around `` `y_delta``` in the above code.

5. Finally (added on August 26, 2017)

I discovered that joyplot was added to the seaborn example last month. https://github.com/mwaskom/seaborn/blob/master/examples/kde_joyplot.py

figure_1.png

We also found joypy. temperatures.png

It would be better to use either of these two.

Recommended Posts

Make a joyplot-like plot of R in python
Make a copy of the list in Python
Make a table of multiplication of each element in a spreadsheet (Python)
Display a list of alphabets in Python 3
Make a relation diagram of Python module
Let's make a combination calculation in Python
Draw a graph of a quadratic function in Python
Get the caller of a function in Python
Rewriting elements in a loop of lists (Python)
Make a rock-paper-scissors game in one line (python)
Output in the form of a python array
Get a glimpse of machine learning in Python
A well-prepared record of data analysis in Python
Try to make a Python module in C language
Make a copy of a Google Drive file from Python
A collection of code often used in personal Python
Make a simple Slackbot with interactive button in python
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
A collection of Excel operations often used in Python
A reminder about the implementation of recommendations in Python
Take a screenshot in Python
Create a function in Python
Equivalence of objects in Python
Don't make test.py in Python!
Make a fortune with Python
Make Opencv available in Python
Make python segfault in 2 lines
Draw a heart in Python
Implementation of quicksort in Python
Plot geographic information in Python
Find out the apparent width of a string in python
[Python] How to make a list of character strings character by character
Get the number of specific elements in a python list
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
[Note] Import of a file in the parent directory in Python
How to get a list of built-in exceptions in python
A memo of writing a basic function in Python using recursion
Comparison of data frame handling in Python (pandas), R, Pig
I tried to make a stopwatch using tkinter in python
A set of script files that do wordcloud in Python3
Find the eigenvalues of a real symmetric matrix in Python
I want to make input () a nice complement in python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
When I got a list of study sessions in Python, I found something I wanted to make
A record of patching a python package
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
Make python segfault in one line
A good description of Python decorators
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Let's make a GUI with python.
Run shell command / python in R
[Python] A memorandum of beautiful soup4
Draw a scatterplot matrix in python
Division of timedelta in Python 2.7 series
A brief summary of Python collections
ABC166 in Python A ~ C problem
MySQL-automatic escape of parameters in python