1. 程式人生 > 實用技巧 >Python和R語言峰巒圖製作

Python和R語言峰巒圖製作

1 Python-joypy 製作峰巒圖

到網址:https://github.com/sbebo/joypy 可下載zip

JoyPy是一個基於 matplotlib+pandas 的單功能Python軟體包,其目的僅在於:繪製Joyplots(又稱脊線圖)。

下載JoyPy包

pip install joypy
#或者是從github下載
git clone [email protected]:sbebo/joypy.git
cd joypy
pip install .

原始資料形式:

import joypy
import pandas as pd
import numpy as np
from
matplotlib import pyplot as plt from matplotlib import cm iris = pd.read_csv("data/iris.csv") %matplotlib inline fig, axes = joypy.joyplot(iris)#連續值的列為一個"脊"

%matplotlib inline
fig, axes = joypy.joyplot(iris, by="Name")#根據"Name"分組,每個Name是一行"脊",其中有多個,預設y軸一致

%matplotlib inline
fig, axes = joypy.joyplot(iris, by="
Name", ylim='own')#使用各自y值 但是這就不可比 建議使用: fig, axes = joypy.joyplot(iris, by="Name", overlap=3)

%matplotlib inline
fig, axes = joypy.joyplot(iris, by="Name", column="SepalWidth",
                          hist=True, bins=20, overlap=0,
                          grid=True, legend=False)

溫度

%matplotlib inline
temp 
= pd.read_csv("data/daily_temp.csv",comment="%") temp.head()

%matplotlib inline

labels=[y if y%10==0 else None for y in list(temp.Year.unique())]#只留下10的倍數的年份 避免太擠了
fig, axes = joypy.joyplot(temp, by="Year", column="Anomaly", labels=labels, range_style='own', #range_style='own'限制x顯示範圍不是所有的x軸
                          grid="y", linewidth=1, legend=True, figsize=(6,5),#grid="y"只顯示y軸 fade=True加上就是顯示原始值而不是估算的kde核密度值
                          title="Global daily temperature 1880-2014 \n(°C above 1950-80 average)",
                          colormap=cm.autumn_r)

2 Python-Matplotlib 製作峰巒圖

https://matplotlib.org/matplotblog/posts/create-ridgeplots-in-matplotlib/

3 R-ggridges製作峰巒圖

https://wilkelab.org/ggridges/

install.packages("ggridges")

    
ggplot(diamonds, aes(x = price, y = cut)) +
  geom_density_ridges(scale = 4) + 
  scale_y_discrete(expand = c(0, 0)) +     # will generally have to set the `expand` option
  scale_x_continuous(expand = c(0, 0)) +   # for both axes to remove unneeded padding
  coord_cartesian(clip = "off") + # to avoid clipping of the very top of the top ridgeline
  theme_ridges()
#> Picking joint bandwidth of 458

參考:

https://mp.weixin.qq.com/s/UBzPDTc3lwCbeI-q1Bfj-w