LoginSignup
2
2

More than 3 years have passed since last update.

matplotlibで三角グラフを作成する

Last updated at Posted at 2019-04-23

この記事を読むとできるようになること
matplotlibで三角グラフが描ける

三角グラフを描くには,Rに用意されているtriangle.plotなどが使えます.
(こちらのページが参考になります→)三角ダイアグラムを描く(Drawing ternary diagrams)
しかし機能に限界があるためPythonで描きたいなあと思いました.

  • 環境
    • macOS Mojave 10.14.4
    • Python 3.7.2

次のスクリプトで,三角グラフの枠が描けます.

triangel.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_aspect('equal', 'datalim')
plt.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)
plt.tick_params(bottom=False, left=False, right=False, top=False)
plt.gca().spines['bottom'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)

h = np.sqrt(3.0)*0.5

#内側目盛
for i in range(1,10):
    ax1.plot([i/20.0, 1.0-i/20.0],[h*i/10.0, h*i/10.0], color='gray', lw=0.5)
    ax1.plot([i/20.0, i/10.0],[h*i/10.0, 0.0], color='gray', lw=0.5)
    ax1.plot([0.5+i/20.0, i/10.0],[h*(1.0-i/10.0), 0.0], color='gray', lw=0.5)

#外周
ax1.plot([0.0, 1.0],[0.0, 0.0], 'k-', lw=2)
ax1.plot([0.0, 0.5],[0.0, h], 'k-', lw=2)
ax1.plot([1.0, 0.5],[0.0, h], 'k-', lw=2)

#頂点のラベル
ax1.text(0.45, h+0.02, 'x', fontsize=16)
ax1.text(-0.1, -0.02, 'y', fontsize=16, rotation=300)
ax1.text(1.03, -0.02, 'z', fontsize=16, rotation=60)

#軸ラベル
for i in range(1,10):
    ax1.text(0.5+(10-i)/20.0, h*(1.0-(10-i)/10.0), '%d0' % i, fontsize=10)
    ax1.text((10-i)/20.0-0.04, h*(10-i)/10.0+0.02, '%d0' % i, fontsize=10, rotation=300)
    ax1.text(i/10.0-0.03, -0.025, '%d0' % i, fontsize=10, rotation=60)

一辺の長さが1の正三角形を描いて,もともとのx軸y軸を消して無理やり描いています笑

点$(x, y, z)$を$(X, Y)$座標に変換してplotするには,

X = z + \frac{x}{2}\\
Y = \frac{\sqrt{3}}{2}x

とすればOKです.

関連記事

matplotlibで三角グラフを作成する 2

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2