Skip to content

Commit

Permalink
add sankey tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
frankligy committed Oct 30, 2022
1 parent e084736 commit 5d21f80
Show file tree
Hide file tree
Showing 4 changed files with 5,716 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ I am planning to share how to make publication-quality figures in python, I will
4. [Tutorial IV (Violin plot, dendrogram)](https://towardsdatascience.com/making-publication-quality-figures-in-python-part-iv-violin-plot-and-dendrogram-ed0bb8b23ddd)
5. [Tutorial V (Seaborn)](https://frankligy.medium.com/all-you-need-to-know-about-seaborn-6678a02f31ff)

## Phase II: Plotly meets scientific figures
## Phase II: Advanced tutorials
1. [Plotly, interactive network](https://frankligy.medium.com/plotly-meets-scientific-visualization-8c2074f032cb)
2. [Sankey plot strategies](https://frankligy.medium.com/the-essence-of-drawing-sankey-plot-491fb2cb234)


## Do you want to know some tricks?
Expand All @@ -21,5 +22,4 @@ I am planning to share how to make publication-quality figures in python, I will
4. [legend,flexibly adjust it](https://github.com/frankligy/python_visualization_tutorial/blob/main/examples/legend.ipynb)
5. [transformation, bbox](https://github.com/frankligy/python_visualization_tutorial/blob/main/examples/extend_ax.ipynb)
6. [stacked legend](https://github.com/frankligy/python_visualization_tutorial/blob/main/examples/stacked_legends.ipynb)
7. [markersize and difference between unit "unit" and "pixel"](https://github.com/frankligy/python_visualization_tutorial/blob/main/examples/markersize.ipynb)
8. stay tuned
7. stay tuned
45 changes: 45 additions & 0 deletions sankey/Sankey_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import PathPatch, Rectangle

# draw the canvas and ax
fig,ax = plt.subplots()
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.grid()

# draw the recs
rec_left = Rectangle(xy=(0.1,0.6),width=0.2,height=0.2,facecolor='orange',edgecolor='k')
ax.add_patch(rec_left)
rec_right = Rectangle(xy=(0.7,0.2),width=0.2,height=0.2,facecolor='green',edgecolor='k')
ax.add_patch(rec_right)

# demo the path object logics
verts = [(0.3,0.8),(0.5,0.8),(0.7,0.4),(0.3,0.6),(0.3,0.8)]
codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
p = Path(verts,codes)
ax.add_patch(PathPatch(p,fc='none'))

# method1, Path, demo the curve4 logics
verts = [(0.3,0.8), (0.5,0.8), (0.5,0.4), (0.7,0.4)]
codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4]
p = Path(verts,codes)
ax.add_patch(PathPatch(p,fc='none',alpha=0.6))

# method1, path
verts = [(0.3,0.8), (0.5,0.8), (0.5,0.4), (0.7,0.4), (0.7,0.2), (0.5,0.2), (0.5,0.6), (0.3,0.6), (0.3,0.8)]
codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CLOSEPOLY]
p = Path(verts,codes)
ax.add_patch(PathPatch(p,fc='red',alpha=0.6))

# method2, convolve
yu = np.array(50*[0.8] + 50*[0.4])
yu_c = np.convolve(yu, 0.05*np.ones(20),mode='valid')
yu_cc = np.convolve(yu_c, 0.05*np.ones(20),mode='valid')
yd = np.array(50*[0.6] + 50*[0.2])
yd_c = np.convolve(yd, 0.05*np.ones(20),mode='valid')
yd_cc = np.convolve(yd_c, 0.05*np.ones(20),mode='valid')
ax.fill_between(np.linspace(0.3,0.7,62),yd_cc,yu_cc,color='blue',alpha=0.6)
Binary file added sankey/sankey_tutorial.pptx
Binary file not shown.
Loading

0 comments on commit 5d21f80

Please sign in to comment.