2015-06-21 125 views
4

我試圖繪製看起來像等邊三角形兩側的刻度線的尖叉。我正在Python中使用Matplotlib。Python(Matplotlib) - 三元邏輯圖上的刻度標記

這裏是等邊三角形***:

import matplotlib.pyplot as plt 
from pylab import * 
fig = figure() 
ax = fig.add_subplot(111) 
ax.axis('equal') 
ax.plot([1, 0.5], [0, 0.866], 'k-') 
ax.plot([0, 0.5], [0, 0.866], 'k-') 
ax.plot([0, 1], [0, 0], 'k-') 
ax.plot([0.5, 1.01, 0.1], [-0.01, 0.5, 0.88], 'w.') #base boundary 
plt.show() 

將所得圖像在這裏看到。 triangle_from_code

現在,我想添加看起來像刻度線的線條。這些線將被繪製,如看到here from 1:07-1:29 and from 2:19-2:32

我想輸出看起來像這個圖像。 enter image description here

有沒有辦法用Matplotlib繪製這些線?編輯

:***三角代碼爲每here

+1

是否有一個原因,你不要簡單把它們畫成線條就像你對三角形做的那樣? – Jakob

+0

是的,我可以做到這一點。但是,我不確定如何將線條放在適當的間距上。這是我遇到的問題,這就是爲什麼我不能將線條添加爲線條。 –

回答

4

這確實或多或少你想要什麼:

from __future__ import division 
import matplotlib.pyplot as plt 
import numpy as np 

def plot_ticks(start, stop, tick, n): 
    r = np.linspace(0, 1, n+1) 
    x = start[0] * (1 - r) + stop[0] * r 
    x = np.vstack((x, x + tick[0])) 
    y = start[1] * (1 - r) + stop[1] * r 
    y = np.vstack((y, y + tick[1])) 
    plt.plot(x, y, 'k', lw=1) 

n = 10 
tick_size = 0.2 
margin = 0.05 

# define corners of triangle  
left = np.r_[0, 0] 
right = np.r_[1, 0] 
top = np.r_[0.5, 3**0.5/2] 
triangle = np.c_[left, right, top, left] 

# define vectors for ticks 
bottom_tick = tick_size * (right - top)/n 
right_tick = tick_size * (top - left)/n 
left_tick = tick_size * (left - right)/n 

plt.plot(triangle[0], triangle[1], 'k', lw=2) 
plot_ticks(left, right, bottom_tick, n) 
plot_ticks(right, top, right_tick, n) 
plot_ticks(left, top, left_tick, n) 
plt.axis([left[0]-margin, right[0]+margin, left[1]-margin, top[1]+margin]) 
plt.gca().set_aspect('equal', adjustable='box') 
plt.show()  

結果: enter image description here

+0

謝謝。它完全按照要求工作。 –