[英]Python (Matplotlib) - Tick marks on ternary plot
I am trying to draw tines that look like tick-marks on the sides of an equilateral triangle. 我试图在等边三角形的两侧绘制看起来像刻度线的尖齿。 I am doing this using Matplotlib in Python.
我在Python中使用Matplotlib这样做。
Here is the equilateral triangle***: 这是等边三角形***:
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()
The resulting image is seen here. 在此处可以看到生成的图像。
.
。
Now, I would like to add lines that look like tick marks. 现在,我想添加看起来像刻度线的线条。 The lines would be drawn like seen here from 1:07-1:29 and from 2:19-2:32 .
从1:07-1:29和2:19-2:32可以看出这些线条。
I would like the output to look something like this image. 我希望输出看起来像这个图像。
Is there a way to draw these lines with Matplotlib? 有没有办法用Matplotlib绘制这些线?
This does more or less what you want: 这或多或少都符合您的要求:
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()
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.