2014-09-08 2 views
0

내 그림의 상단 근처에 범례를 표시하려고에 제목과 범례를 사용자 정의 (this을 같은)과 그 위의 제목 (this 같은) 그러나파이썬은 그림

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(1,24,1) 
y = np.array([400,650,1020,1300,1600,1950,2200,2550,2850,3150,3400,3550,3800,3950,4050,4150,4210,4250,4300,4320,4310,4300,4200]) 

fig = plt.figure() 
ax = plt.subplot(111) 

ax.plot(x,y,'o', label='data set 1') 
plt.text(0.5, 1.4, 'Data set A-1', 
     horizontalalignment='center', 
     fontsize=16, 
     transform = ax.transAxes) 
ax.set_xlabel('x', fontsize=14) 
ax.set_ylabel('y', fontsize=14) 
ax.set_ylim(ymax=5000) 

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.25), 
      ncol=3, fancybox=True, shadow=True) 
#plt.tight_layout() 
plt.show() 

fig.savefig('test.pdf') 

,이 그래프를 저장할 때 제목과 범례가 모두 누락되었습니다. 축과 데이터 포인트 만 pdf에 저장됩니다.

+0

당신이 사용하는 파이썬의 버전? –

+0

Python 2.7.7, Spyder 2.3.0 (하지만 Spyder와 아무런 관련이 없음) –

+0

이들은 그림의 관점에서 볼 때 너무 멀리 위에 있습니다. 숫자가 작아 지도록 크기를 조정하면 나타나기 시작합니다. – Ajean

답변

2

보이는 영역 밖으로 이동하고 있습니다. set_title mpl을 사용하는 경우 배치가주의해야합니다. 전설에 경계 상자와 동일 :

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(1,24,1) 
y = np.array([400,650,1020,1300,1600,1950,2200,2550,2850,3150,3400,3550,3800,3950,4050,4150,4210,4250,4300,4320,4310,4300,4200]) 

fig = plt.figure() 
ax = plt.subplot(111) 

ax.plot(x,y,'o', label='data set 1') 
ax.set_title('Data set A-1') 

ax.set_xlabel('x', fontsize=14) 
ax.set_ylabel('y', fontsize=14) 
ax.set_ylim(ymax=5000) 

ax.legend(loc='upper center', 
      ncol=3, fancybox=True, shadow=True) 
#plt.tight_layout() 
plt.show() 

enter image description here

당신은 아마도 legend에 kwarg로 num_points=1을 전달하려는.

+0

정확하게 원했던 것은 아니지만, 여전히 꽤 좋아 보입니다! –

1

축에도 범례를 표시 하시겠습니까?

enter image description here

#!/usr/bin/python3 

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(1,24,1) 
y = np.array([400,650,1020,1300,1600,1950,2200,2550, 
       2850,3150,3400,3550,3800,3950,4050,4150, 
       4210,4250,4300,4320,4310,4300,4200]) 

fig = plt.figure() 
ax = plt.subplot(111) 

ax.plot(x,y,'o', label='data set 1') 

ax.set_xlabel('x', fontsize=14) 
ax.set_ylabel('y', fontsize=14) 
ax.set_ylim(ymax=5000) 


ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), 
      ncol=3, fancybox=True, shadow=True) 
title = ax.set_title('Data set A-1') 

fig.tight_layout() 

fig.subplots_adjust(top=0.85) 
title.set_y(1.07) 

fig.savefig("13.png")