programing

산점도:오른쪽의 lib-axis

subpage 2023. 9. 1. 20:59
반응형

산점도:오른쪽의 lib-axis

플롯의 오른쪽에 y축 레이블을 붙이는 간단한 방법이 있습니까?눈금 레이블에 대해 다음을 사용하여 수행할 수 있음을 알고 있습니다.ax.yaxis.tick_right()하지만 축 라벨에 대해서도 가능한지 알고 싶습니다.

한 가지 생각이 떠오른 것은 것은 사용하는 것이었습니다.

ax.yaxis.tick_right()
ax2 = ax.twinx()
ax2.set_ylabel('foo')

그러나 y축의 범위를 유지하면서 모든 레이블(눈금 및 축 레이블)을 오른쪽에 배치하는 효과는 없습니다.간단히 말해서 저는 모든 y축 라벨을 왼쪽에서 오른쪽으로 이동하는 방법을 원합니다.

다음과 같은 기능을 사용할 수 있습니다.

ax.yaxis.set_label_position("right")
ax.yaxis.tick_right()

예를 보려면 여기를 참조하십시오.

에 제시된 예를 따르려면matplotlib그리고 축의 양쪽에 레이블이 있지만 사용하지 않고 그림을 만듭니다.subplots()함수, 여기 내 해결책이 있습니다.

from matplotlib import pyplot as plt
import numpy as np

ax1 = plt.plot()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
plt.plot(t,s1,'b-')
plt.xlabel('t (s)')
plt.ylabel('exp',color='b')

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
plt.ylabel('sin', color='r')
plt.show()

이전 답변이 최신 버전이 아닙니다.다음은 위의 예제에 대한 최신 코드입니다.

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()

여기서.

(질문을 다시 시작해서 죄송합니다)

비열한 속임수인 건 알지만 도끼 다루는 일로 내려가기 싫다면 안에 있어요plt명령을 사용할 수 있습니다.labelpad그래프 측면의 오른쪽에 레이블을 배치하는 스칼라 인수입니다.약간의 시행착오를 거친 후에 작동하며, 정확한 스칼라 값은 그림 크기의 치수와 관련이 있을 수 있습니다.

예:

# move ticks
plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)

# move label
plt.ylabel('Your label here', labelpad=-725, fontsize=18)

언급URL : https://stackoverflow.com/questions/13369888/matplotlib-y-axis-label-on-right-side

반응형