我汇总了所有三元相图(ternary plots)的绘制方法,超实用!!

作为2022年的第一篇推文,我们读者要求,介绍如何使用Python和R制作三相元图( ternary plots),涉及的知识点如下:

Python-ternary包绘制三元相图R-ggtern包绘制三元相图Python-ternary包绘制三元相图在查阅“使用Python绘制三元相图”时,我们查阅到了ternary包,该包可实现使用Python绘制三元相图的要求,官网为:https://github.com/marcharper/python-ternary,我们绘制几副官网的图例,其他样例,大家可以参考官网:

样例一:Simplex Boundary and Gridlines

代码语言:javascript代码运行次数:0运行复制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

代码语言:javascript代码运行次数:0运行复制import math

def color_point(x, y, z, scale):

w = 255

x_color = x * w / float(scale)

y_color = y * w / float(scale)

z_color = z * w / float(scale)

r = math.fabs(w - y_color) / w

g = math.fabs(w - x_color) / w

b = math.fabs(w - z_color) / w

return (r, g, b, 1.)

def generate_heatmap_data(scale=5):

from ternary.helpers import simplex_iterator

d = dict()

for (i, j, k) in simplex_iterator(scale):

d[(i, j, k)] = color_point(i, j, k, scale)

return d

fig,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包的基本探索,具体如下:

数据构建如下:

代码语言:javascript代码运行次数:0运行复制test_data = data.frame(x = runif(100),

y = runif(100),

z = runif(100))

head(test_data)

预览如下:

point charts:

代码语言:javascript代码运行次数:0运行复制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: GGtern Test",

subtitle = "processed map charts with ggtern()",

caption = "Visualization by DataCharm") +

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),

)

可视化结果如下:

优化处理:

代码语言:javascript代码运行次数:0运行复制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: GGtern Test",

subtitle = "processed map charts with ggtern()",

caption = "Visualization by DataCharm") +

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绘制了三元相图,整体难度较低,小伙伴们可行自己参考官网进行探索。接下来,我们还会进行优质数据的免费分享哦!

西游杀怎么玩(西游杀卡牌游戏规则)
黄瓜切了很多丝吃不完,黄瓜丝怎么保存呢?