2013-08-16 190 views
0

我有一些代碼繪製散點圖上的數據點,並繪製兩條線。我想讓這些線條走到繪圖的邊緣,以便它們在包含一些數據點的左上角創建一個框。然而,情節總是在行尾之外增加一些空間,所以我只有兩條直線而不是一個方框。有誰知道如何阻止它添加這個空間,以便我的水平線到y軸,我的垂直線到圖的頂部,創建一個盒子?python散點圖繪製線

我的代碼是

import pyfits 
import numpy as np 
import math 
from numpy.lib import scimath 
scimath.log(-math.exp(1)) == (1+1j*math.pi) 
import matplotlib.pyplot as plt 

###---COLOUR COLOUR PLOTS 

path2 = "/data/nhine/colour_plots/" 


##my data 
hdulist = pyfits.open(path2 + 'colour_plots_z1_final.fits') 
hdulist1 = pyfits.open(path2 + 'colour_plots_z1_5_final.fits') 
hdulist2 = pyfits.open(path2 + 'colour_plots_z5_final.fits') 

table = hdulist[1] 
table1 = hdulist1[1] 
table2 = hdulist2[1] 

col1_1 = np.array(table.data.field('col1')) #KAB - IRAC1 
col2_1 = np.array(table.data.field('col2')) #HAB - KAB 
col3_1 = np.array(table.data.field('col3')) #REDMAG 
col4_1 = np.array(table.data.field('col4')) #IRAC1-IRAC2 
col1_15 = np.array(table1.data.field('col1')) 
col2_15 = np.array(table1.data.field('col2')) 
col3_15 = np.array(table1.data.field('col3')) 
col4_15 = np.array(table1.data.field('col4')) 
col1_5 = np.array(table2.data.field('col1')) 
col2_5 = np.array(table2.data.field('col2')) 
col3_5 = np.array(table2.data.field('col3')) 
col4_5 = np.array(table2.data.field('col4')) 

##lines 
x = np.array([1.6,1.6,1.6,1.6,1.6,1.6,]) 
y = np.array([2.15,2.25,2.4,2.5,2.6,2.7,]) 
w = np.array([0.7,0.8,1.0,1.2,1.4,1.6]) 
z = np.array([2.15,2.15,2.15,2.15,2.15,2.15]) 

##C12 data 

qso6a = np.array([1.5]) 
qso6b = np.array([2.25]) 
gal6a = np.array([0.7]) 
gal6b = np.array([2.4]) 
all35a = np.array([1.5,1.16,1.8,2.4,2.55,2.6,2.7,2.78,2.9]) 
all35b = np.array([2,1.6,1.9,1.35,0.9,1.25,1.65,1.1,1.3]) 
less1a = np.array([2.4,2.9,3.3,3.309]) 
less1b = np.array([1.3,0.7,1.75,1.35]) 

##plot 

from pylab import * 


fig = figure() 
ax1 = fig.add_subplot(111) 

ax1.scatter(col2_5, col1_5, s=50, c='g', marker="s", label='Targets at z>5') 
ax1.scatter(gal6a, gal6b, s=50, c='g', marker="o", label='C12 galaxy at z~6') 
ax1.scatter(qso6a, qso6b, s=50, c='g', marker="d", label='C12 QSO at z~6') 
ax1.scatter(col2_15, col1_15, s=50, c='r', marker="s", label='Targets at 1<z<5') 
ax1.scatter(all35a, all35b, s=50, c='r', marker="o", label='C12 at 3<z<5') 
ax1.scatter(col2_1, col1_1, s=50, c='b', marker="s", label='Targets at z<1') 
ax1.scatter(less1a, less1b, s=50, c='b', marker="o", label='C12 z<1') 

plot(x,y, color='b') 
plot(w,z, color='b') 
legend(loc='best', numpoints=1, ncol=1, fontsize=8) 
ttext = title('Colour-colour plot') 
ytext = ylabel('K - IRAC1') 
xtext = xlabel('H - K') 
setp(ttext, size='large', color='black', style='italic') 
setp(xtext, size='medium', weight='light', color='black') 
setp(ytext, size='medium', weight='light', color='black') 
show() 

hdulist.close() 
hdulist1.close() 
hdulist2.close() 

回答

0

您可以防止軸從當你畫你的「盒子」使用plot(...,scalex=False,scaley=False)被重新調整。

不過,我會建議你使用像一個Rectangle勾勒出你的觀點,例如:

from matplotlib.pyplot import Rectangle 
mybox = Rectangle((0.7,2.15), 0.9, 0.55, fill=False, ec='b') 
ax1.add_artist(mybox) 

否則,你可以看看matplotlibtools for annotation,其中包括各種花哨的方框和箭頭。

+0

謝謝,那真的很好。 – nancyh