홈>
예를 들어, 해당 색상으로 다음과 같은 좌표가 육각형 육각형 격자를 나타냅니다.
coord = [[0,0,0],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0],[1,0,-1]]
colors = [["Green"],["Blue"],["Green"],["Green"],["Red"],["Green"],["Green"]]
Plot에서 점이 육각형 모양을 유지하도록 파이썬에서 어떻게 이것을 그릴 수 있습니까? 또한 육각형의 '색상'목록을 나타내는 방법은 무엇입니까?
일부 :
간단한 육각형 격자
그러나 모양은 중요하지 않습니다. 단순한 산점도 유형 시각화만으로 충분합니다. 다른 육각형과 관련하여 색상이 어디에 있는지 알 수 있습니다.
- 답변 # 1
- 답변 # 2
(u, v, w) 16 진 좌표의 튜플을 직사각형 좌표로. 표준
turtle
를 사용하여 설명하겠습니다. 모듈 (matplotlib 모듈이 없습니다). 각 점이 올바른 위치에 그려져 있는지 쉽게 확인할 수 있도록 목록에서 색상을 변경했습니다.import turtle from math import sqrt root3 = sqrt(3) # the scale used for drawing side = 50 # Convert hex coordinates to rectangular def hex_to_rect(coord): u, v, w = coord x = u - v/2 - w/2 y = (v - w) * root3 / 2 return x * side, y * side # Initialize the turtle t = turtle.Turtle() t.speed(0) t.hideturtle() t.up() coords = [[0,0,0], [0,1,-1], [-1,1,0], [-1,0,1], [0,-1,1], [1,-1,0], [1,0,-1]] colors = ['black', 'red', 'orange', 'green', 'cyan', 'blue', 'magenta'] #Plot the points for hexcoord, color in zip(coords, colors): xy = hex_to_rect(hexcoord) t.goto(xy) t.dot(15, color) # Wait for the user to close the window turtle.done()
출력
- 답변 # 3
아래는 PM2Ring의 거북이를 끝내려는 시도입니다 기반 솔루션 (+1)과 그의 대답에서 좌표 계산 오류로 보이는 것을 수정하십시오.
from math import sqrt from turtle import Turtle, Screen ROOT3_OVER_2 = sqrt(3) / 2 FONT_SIZE = 18 FONT = ('Arial', FONT_SIZE, 'normal') SIDE = 50 # the scale used for drawing # Convert hex coordinates to rectangular def hex_to_rect(coord): v, u, w = coord x = -u / 2 + v - w / 2 y = (u - w) * ROOT3_OVER_2 return x * SIDE, y * SIDE def hexagon(turtle, radius, color, label): clone = turtle.clone() # so we don't affect turtle's state xpos, ypos = clone.position() clone.setposition(xpos - radius / 2, ypos - ROOT3_OVER_2 * radius) clone.setheading(-30) clone.color('black', color) clone.pendown() clone.begin_fill() clone.circle(radius, steps=6) clone.end_fill() clone.penup() clone.setposition(xpos, ypos - FONT_SIZE / 2) clone.write(label, align="center", font=FONT) # Initialize the turtle tortoise = Turtle(visible=False) tortoise.speed('fastest') tortoise.penup() coords = [[0, 0, 0], [0, 1, -1], [-1, 1, 0], [-1, 0, 1], [0, -1, 1], [1, -1, 0], [1, 0, -1]] colors = ["Green", "Blue", "Green", "Green", "Red", "Green", "Green"] labels = ['yes', 'no', 'yes', 'no', 'yes', 'no', 'no'] # Plot the points for hexcoord, color, label in zip(coords, colors, labels): tortoise.goto(hex_to_rect(hexcoord)) hexagon(tortoise, SIDE, color, label) # Wait for the user to close the window screen = Screen() screen.exitonclick()
관련 질문
- python : TypeError: matplotlib에서 플롯의 정수 스칼라 배열만 스칼라 인덱스로 변환할 수 있습니다.
- python : 팬더 사용자 정의 데이터 포인트로 Seaborn Violinplot을 만드는 방법은 무엇입니까?
- python : 텍스트를 기울임꼴로 만든 후 Figsize 및 축 선 너비가 갑자기 작아짐
- "ValueError: Shape Mismatch"로 이어지는 Python 오류 캐스케이드
- python : pandas DataFrame 플롯 xticks 간격이 작동하지 않습니다.
- python : matplotlib의 vlines에서 한 줄의 색상을 변경할 수 있습니까?
- python : Matplotlib pyplot stackplot은 앤티앨리어싱 시 아티팩트를 생성합니다.
- python : matplotlib를 사용하는 파이썬에서 matplotlib.pyplot.xlabel()과 .set_xlabel()의 차이점은 무엇입니까?
- python : 지도에서 경로 근처의 가장 가까운 좌표 찾기
- python : matplotlib를 사용하여 3D Numpy 배열을 플로팅하는 분산형
당신은 단지
(y, z)
를 돌리기 만하면됩니다 육각형에서y
로 좌표 matplotlib 축의 직교 좌표입니다.올바른 방법은이 공식을 사용하는 것입니다 :
그런 다음 matplotlibRegularPolygon
를 사용하여 육각형을 추가 할 수 있습니다scatter
를 사용하여 중심을 패치하거나 플롯하십시오. .다음은 목록에서 플롯을 작성하는 스크립트입니다.