简体   繁体   中英

drawing line on scatter graph in python

I have some code that plots data points on a scatter graph and draws two lines on it. I want the lines to go to the edge of the plot so they create a box in the top left corner containing some of the data points. However, the plot always adds some space beyond the end of the lines so that I just have two lines at right angles rather than a box. Does anyone know how to stop it adding this space, so that my horizontal line goes to the y axis and my vertical line to the top of the plot, creating a box?

My code is

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

You can prevent the axes from being rescaled when you draw your 'box' using plot(...,scalex=False,scaley=False) .

However, I would suggest you use something like a Rectangle to outline your points, eg:

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

Or else you could take a look at matplotlib 's tools for annotation that include various fancy boxes and arrows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM