아래 이미지와 같이 Matplotlib로 축 레이블 (x 및 y)을 추가하고 y 축 번호를 회전하는 방법은 무엇입니까?
plt.yticks(rotation=45)
y 축 번호를 회전 하려고했지만 고려되지 않았습니다.
게다가, 나는 또한 내 예제 코드에서 두 개가 아닌 하나의 0과 직사각형 대신 정사각형 격자를 가지려고 노력하고 있습니다.
from mpl_toolkits.axisartist.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
ax.axis[direction].set_axisline_style('->')
# adds X and Y-axis from the origin
ax.axis[direction].set_visible(True)
ax.axis['yzero'].set_ticklabel_direction("-")
for direction in ["left", "right", "bottom", "top"]:
# hides borders
ax.axis[direction].set_visible(False)
x = np.linspace(-5, 5, 100)
ax.plot(x, -x**2+16, color="#ab74a6", linewidth=3)
plt.title(r'$y = -x^2+16$')
plt.yticks(rotation=45)
plt.axis([-5, 5, -10, 20])
plt.grid(True)
plt.show()
다음은 spines
대신 사용하는 작업 코드 예제입니다 SubplotZero
.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
x = np.linspace(-5, 5, 100)
ax.plot(x, -x**2+16, color="#ab74a6", linewidth=3)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# hide one of the zero labels and adjust the other
ax.yaxis.get_major_ticks()[3].label1.set_visible(False)
ax.xaxis.get_major_ticks()[3].label1.set_horizontalalignment("right")
ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)
ax.axis('equal')
ax.set_xlabel('x', position=(1,0), ha='right')
ax.set_ylabel('y', position=(0,1), ha='right', rotation=0)
plt.title(r'$y = -x^2+16$', y=1.08)
plt.grid(True)
plt.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다