Python和R语言峰峦图制作

1 Python-joypy 制作峰峦图

到网址:https://github.com/sbebo/joypy    可下载zip

JoyPy是一个基于 matplotlib+pandas 的单功能Python软件包,其目的仅在于:绘制Joyplots(又称脊线图)。

 下载JoyPy包

pip install joypy
#或者是从github下载
git clone git@github.com: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/

1
2
3
4
5
6
7
8
9
10
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

 

1
2
3
4
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = Month, fill = stat(x))) +
  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +
  scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
  labs(title = 'Temperatures in Lincoln NE in 2016')

个人认为此图还能通过渐变颜色映射反映分布值的大小

 可以做出很多调整:https://wilkelab.org/ggridges/articles/introduction.html 

1
2
3
4
5
6
7
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = Species)) +
  geom_density_ridges(
    aes(point_color = Species, point_fill = Species, point_shape = Species),
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  scale_point_color_hue(l = 40) +
  scale_discrete_manual(aesthetics = "point_shape", values = c(21, 22, 23))

另外一个风格:

https://www.ershicimi.com/p/fbc80ca437cbbdd6fed53ebda13719cd

github数据和代码下载:https://github.com/zonination/perceptions

原始数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#Import files, load plot and data packages, fire up the number machine.
# setwd("~/Dropbox/R/Perceptions of Probability")
probly <- read.csv("probly.csv", stringsAsFactors=FALSE)
numberly <- read.csv("numberly.csv", stringsAsFactors=FALSE)
library(tidyverse)
library(ggjoy)
library(scales)
setwd("G:/RWORK/perceptions-master")#先改变工作空间目录
#Melt data into column format.将数据融合至两列,一列是变了名称 一列是值
numberly <- gather(numberly, "variable", "value", 1:10)
numberly$variable <- gsub("[.]"," ",numberly$variable)#把.用空格置换
probly <- gather(probly, "variable", "value", 1:17)
probly$variable <- gsub("[.]"," ",probly$variable)
probly$value<-probly$value/100 # convert to %
 
#Order in the court!按照想要的顺序排序
probly$variable <- factor(probly$variable,
                          c("Chances Are Slight",
                            "Highly Unlikely",
                            "Almost No Chance",
                            "Little Chance",
                            "Probably Not",
                            "Unlikely",
                            "Improbable",
                            "We Doubt",
                            "About Even",
                            "Better Than Even",
                            "Probably",
                            "We Believe",
                            "Likely",
                            "Probable",
                            "Very Good Chance",
                            "Highly Likely",
                            "Almost Certainly"))
numberly$variable <- factor(numberly$variable,
                            c("Hundreds of",
                              "Scores of",
                              "Dozens",
                              "Many",
                              "A lot",
                              "Several",
                              "Some",
                              "A few",
                              "A couple",
                              "Fractions of"))
 
#Modify Theme:
source("ztheme.R")
 
#Plot probability data
ggplot(probly,aes(variable,value))+
  geom_boxplot(aes(fill=variable),alpha=.5)+
  geom_jitter(aes(color=variable),size=3,alpha=.2)+
  scale_y_continuous(breaks=seq(0,1,.1), labels=scales::percent)+
  guides(fill=FALSE,color=FALSE)+
  labs(title="Perceptions of Probability",
       x="Phrase",
       y="Assigned Probability",
       caption="created by /u/zonination")+
  coord_flip()+
  z_theme()
ggsave("plot1.png", height=8, width=10, dpi=120, type="cairo-png")
 
#Plot numberly data
ggplot(numberly,aes(variable,value))+
  geom_boxplot(aes(fill=variable),alpha=0.5)+
  geom_jitter(aes(color=variable),size=3,alpha=.2)+
  scale_y_log10(labels=trans_format("log10",math_format(10^.x)),
                breaks=10^(-2:6))+
  guides(fill=FALSE,color=FALSE)+
  labs(title="Perceptions of Probability",
       x="Phrase",
       y="Assigned Number",
       caption="created by /u/zonination")+
  coord_flip()+
  z_theme()
ggsave("plot2.png", height=5, width=8, dpi=120, type="cairo-png")
 
# Joyplot for probly
ggplot(probly,aes(y=variable,x=value))+
  geom_joy(scale=4, aes(fill=variable), alpha=3/4)+
  scale_x_continuous(breaks=seq(0,1,.1), labels=scales::percent)+
  guides(fill=FALSE,color=FALSE)+
  labs(title="Perceptions of Probability",
       y="",
       x="Assigned Probability",
       caption="created by /u/zonination")+
  z_theme()
ggsave("joy1.png", height=8, width=10, dpi=120, type="cairo-png")
 
#Joyplot for numberly
ggplot(numberly,aes(y=variable,x=value))+
  geom_joy(aes(fill=variable, alpha=3/4))+
  scale_x_log10(labels=trans_format("log10",math_format(10^.x)),
                breaks=10^(-2:6))+
  guides(fill=FALSE,color=FALSE)+
  labs(title="Perceptions of Probability",
       x="Assigned Number",
       y="",
       caption="created by /u/zonination")+
  z_theme()
ggsave("joy2.png", height=5, width=8, dpi=120, type="cairo-png")

   

 

参考:

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

posted @   icydengyw  阅读(2393)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示