Я использовал следующий код для создания осей и набора вершин. для многоугольника:
Код: Выделить всё
import ternary
import numpy as np
# Define the scale (the sum of the three components for each point)
scale = 100
# Define the vertices of the polygon (each point should sum to the scale value)
polygon_vertices = [
(20, 30, 50),
(40, 30, 30),
(30, 60, 10),
(10, 50, 40)
]
# Create a figure with a ternary axis
figure, tax = ternary. Figure(scale=scale)
tax.set_title("Ternary Plot with Polygon Contour", fontsize=20)
# Plot the vertices of the polygon on the ternary plot
tax.scatter(polygon_vertices, marker='o', color='red', label='Vertices')
# Connect the vertices to form the contour of the polygon
for i in range(len(polygon_vertices)):
next_i = (i + 1) % len(polygon_vertices)
tax. Plot([polygon_vertices[i], polygon_vertices[next_i]], color='blue', linewidth=2, linestyle='--')
# Fill the polygon area using the ax. Fill method
tax.ax.fill(*zip(*polygon_vertices), color='lightblue', alpha=0.8, label='Polygon Area')
# Set axis labels
tax.left_axis_label("Component 1", fontsize=15)
tax.right_axis_label("Component 2", fontsize=15)
tax.bottom_axis_label("Component 3", fontsize=15)
tax.get_axes().axis('off')
# Set the gridlines
tax.gridlines(color="blue", multiple=10)
# Set ticks and gridlines
tax.ticks(axis='lbr', linewidth=1, multiple=10)
tax.gridlines(multiple=10, color="black")
# Display the legend
tax.legend()
tax.get_axes().set_aspect(1) #sets as equilateral triangle, needs to be the last step of plotting
tax._redraw_labels() #see above
# Display the plot
tax.show()
[img]https: //i.sstatic.net/mL4PCEYD.png[/img]
Как видно на графике, он рисует заполненный многоугольник, но не в правильном месте, и рисует два из них. . Я не уверен, почему. Как мне заполнить правильную область?
Примечание. Это не тот же вопрос, что и многоугольник заполнения Python, поскольку это декартовы координаты. Когда я пытаюсь построить график с помощью add_patches, я получаю ошибку типа, поскольку Polygon может принимать только два позиционных аргумента, но я предоставляю ему три, несмотря на более раннее преобразование троичных координат в декартовы.
Подробнее здесь: https://stackoverflow.com/questions/787 ... rnary-plot