在python中的散点图上绘制线条

时间:2013-08-16 09:27:51

标签: python matplotlib scatter-plot

我有一些代码可以在散点图上绘制数据点并在其上绘制两条线。我希望线条到达图的边缘,以便它们在左上角创建一个包含一些数据点的框。然而,情节总是在线的末端之外增加一些空间,这样我只有两条直线而不是一个方框。有谁知道如何阻止它添加这个空间,以便我的水平线到达y轴,我的垂直线到图的顶部,创建一个盒子?

我的代码是

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()

1 个答案:

答案 0 :(得分: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)

或者您可以查看matplotlib的{​​{3}},其中包含各种精美的方框和箭头。