前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python-R-三相元图(ternary plots)绘制

Python-R-三相元图(ternary plots)绘制

作者头像
DataCharm
发布2021-02-22 11:44:25
1.9K0
发布2021-02-22 11:44:25
举报

作为2021年的第一篇推文,我们选择粉丝要求的图表绘制,这期推文,我们将介绍如何使用Python和R制作三相元图( ternary plots),涉及的知识点如下:

  • Python-ternary包绘制三相元图
  • R-ggtern包绘制三相元图

Python-ternary包绘制三相元图

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

样例一:Simplex Boundary and Gridlines

代码语言:javascript
复制
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
复制
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
复制
test_data = data.frame(x = runif(100),
                       y = runif(100),
                       z = runif(100))
head(test_data)

预览如下:

point charts:

代码语言:javascript
复制
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),
        )

可视化结果如下:

优化处理:

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python-ternary包绘制三相元图
  • R-ggtern包绘制三相元图
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档