0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonでcsvファイル等から三角図を出力する

Posted at

材料系で頻出グラフの三角プロットをpythonで行うときのメモ。

モジュールのインストール

今回紹介するのは、mpltern
https://mpltern.readthedocs.io/

pipでのインストールは、以下の通り。

pip install mpltern

実例

デモデータ

たとえばExcel等で以下のようなデータが入っているとする。
image.png

そして、それが、data.csvとして保存されているとする。
なお、例題のミソとして、文字列と数値が混在しているものを用意した。ただし文字列にスペースは入ってはいけない。

コード

import numpy as np
import matplotlib.pyplot as plt
import mpltern

f = np.loadtxt("data.csv",delimiter=",",dtype = "unicode")
ff = np.zeros(f.shape,"float32")
ff[1:10,1:10]=f[1:10,1:10].astype("float")


top=4   #for Si
left=2  #for Mg
right=6 #for Fe

ax = plt.subplot(projection="ternary")

for i in range(1,6):
  ax.scatter(ff[top][i],ff[left][i],ff[right][i], alpha=0.5, label="filt"+f[0][i])

ax.set_tlabel(str(f.T[0][top]),fontsize=16)
ax.set_llabel(str(f.T[0][left]),fontsize=16)
ax.set_rlabel(str(f.T[0][right]),fontsize=16)

ax.legend()

plt.show()

出力

image.png

コメント

文字列として読み込み

f = np.loadtxt("data.csv",delimiter=",",dtype = "unicode")
ff = np.zeros(f.shape,"float32")
ff[1:10,1:10]=f[1:10,1:10].astype("float")

csvの読み取りにはnp.loadtxtをつかいます。
delimiter=","で、カンマ区切り、
dtype = "unicode"で文字列として読み込みです。

その後、2,3行目で、数値だけの配列を作成します。

文字列として読み込むことで、凡例やラベルに使えるようにします。

軸の選択

ここでは、Si,Mg,Feを軸として使うための指定をします。
わざわざ定数化することにより、汎用性を持たせています。

top=4   #for Si
left=2  #for Mg
right=6 #for Fe

グラフ描画の本体

ax = plt.subplot(projection="ternary")

for i in range(1,6):
  ax.scatter(ff[top][i],ff[left][i],ff[right][i], alpha=0.5, label="filt"+f[0][i])

ax.set_tlabel(str(f.T[0][top]),fontsize=16)
ax.set_llabel(str(f.T[0][left]),fontsize=16)
ax.set_rlabel(str(f.T[0][right]),fontsize=16)

ax.legend()

plt.show()

for文を使うことで、データ点を増やしている

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?