这期推文,我们将介绍如何使用Python和R制作三相元图( ternary plots),涉及的知识点如下:
-
Python-ternary包绘制三相元图
-
R-ggtern包绘制三相元图
-
所有完整代码都已整理之我们的线上课程,有需要的同学+v yidianshuyulove 咨询
Python-ternary包绘制三相元图
在查阅“使用Python绘制三相元图”时,我们查阅到了ternary包,该包可实现使用Python绘制三相元图的要求,官网为:https://github.com/marcharper/python-ternary,我们绘制几副官网的图例,其他样例,大家可以参考官网:
样例一:Simplex Boundary and Gridlines
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
## Boundary and Gridlines
fig,ax = plt.subplots()
scale = 30
figure, tax = ternary.figure(scale=scale,ax=ax)
figure.set_size_inches(6, 6)# Draw Boundary and Gridlines
tax.boundary(linewidth=1.5)
tax.gridlines(color="black", multiple=6)
tax.gridlines(color="blue", multiple=2, linewidth=0.5)# Set Axis labels and Title
fontsize = 12
tax.set_title("Simplex Boundary and Gridlines\n", fontsize=fontsize)
tax.left_axis_label("Left label $\\alpha^2$", fontsize=fontsize, offset=0.14)
tax.right_axis_label("Right label $\\beta^2$", fontsize=fontsize, offset=0.14)
tax.bottom_axis_label("Bottom label $\\Gamma - \\Omega$", fontsize=fontsize, offset=0.14)# Set ticks
tax.ticks(axis='lbr', linewidth=1, multiple=5, offset=0.03)# Remove default Matplotlib Axes
tax.clear_matplotlib_ticks()
tax.get_axes().axis('off')ax.text(.83,-.06,'\nVisualization by DataCharm',transform = ax.transAxes,ha='center', va='center',fontsize = 8,color='black')
ternary.plt.show()
可视化结果如下:
样例二:RGBA colors
import math
def color_point(x, y, z, scale):w = 255x_color = x * w / float(scale)y_color = y * w / float(scale)z_color = z * w / float(scale)r = math.fabs(w - y_color) / wg = math.fabs(w - x_color) / wb = math.fabs(w - z_color) / wreturn (r, g, b, 1.)def generate_heatmap_data(scale=5):from ternary.helpers import simplex_iteratord = dict()for (i, j, k) in simplex_iterator(scale):d[(i, j, k)] = color_point(i, j, k, scale)return dfig,ax = plt.subplots()
scale = 80
data = generate_heatmap_data(scale)
figure, tax = ternary.figure(scale=scale,ax=ax)
figure.set_size_inches(6, 6)
tax.heatmap(data, style="hexagonal", use_rgba=True, colorbar=False)
# Remove default Matplotlib Axes
tax.clear_matplotlib_ticks()
tax.get_axes().axis('off')
tax.boundary()
tax.set_title("RGBA Heatmap")
ax.text(.83,.06,'\nVisualization by DataCharm',transform = ax.transAxes,ha='center', va='center',fontsize = 8,color='black')
plt.show()
可视化结果如下:
除了以上两个较常用的样例,官网还提供如下可视化样例(更多样例,大家可参考官网):
Heatmaps1
Heatmaps2
Heatmaps3
R-ggtern包绘制三相元图
在介绍了Python 绘制三相元图之后,我们再介绍使用R绘制,由于ggplot2的强大功能,我们还是选择ggplot2体系的第三方包进行绘制,而ggtern包则是我们的首要选择。官网:http://www.ggtern.com/。我们虚构数据进行ggtern包的基本探索,具体如下:
数据构建如下:
test_data = data.frame(x = runif(100),y = runif(100),z = runif(100))
head(test_data)
预览如下:
point charts:
library(tidyverse)
library(ggtern)
library(hrbrthemes)
library(ggtext)test_plot_pir <- ggtern(data = test_data,aes(x, y, z))+geom_point(size=2.5)+theme_rgbw(base_family = "Roboto Condensed") +labs(x="",y="",title = "Example Density/Contour Plot: <span style='color:#D20F26'>GGtern Test</span>",subtitle = "processed map charts with <span style='color:#1A73E8'>ggtern()</span>",caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +guides(color = "none", fill = "none", alpha = "none")+theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",size = 20, margin = margin(t = 1, b = 12)),plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),plot.caption = element_markdown(face = 'bold',size = 12),)
可视化结果如下:
优化处理:
test_plot <- ggtern(data = test_data,aes(x, y, z),size=2)+stat_density_tern(geom = 'polygon',n = 300,aes(fill = ..level..,alpha = ..level..))+geom_point(size=2.5)+theme_rgbw(base_family = "Roboto Condensed") +labs(x="",y="",title = "Example Density/Contour Plot: <span style='color:#D20F26'>GGtern Test</span>",subtitle = "processed map charts with <span style='color:#1A73E8'>ggtern()</span>",caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +scale_fill_gradient(low = "blue",high = "red") +#去除映射属性的图例guides(color = "none", fill = "none", alpha = "none")+ theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",size = 20, margin = margin(t = 1, b = 12)),plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),plot.caption = element_markdown(face = 'bold',size = 12),)
可视化结果如下:
除此之外,官网还提供如下样例:
PPS 3-State Model
using geom_label_viewport
Ternary Tribin
Demonstration of Raster Annotation
当然,还有一个交互式的demo可以更好的体验ggtern包的强大,界面如下:
总结
本期推文我们汇总了Python和R绘制了三相元图,整体难度较低,小伙伴们可行自己参考官网进行探索。接下来,我们还会进行优质数据的免费分享哦!