Advice on how to get started with 2D-plotting ?

CM cmpython at gmail.com
Wed Sep 7 07:56:10 CEST 2011


> Now, for my work, I would need to learn the basics fast, for a one-time
> quick-n-dirty job.
>
> This involves a graphic comparison of RFC1918 IP subnets allocation across
> several networks.
>
> The idea is to draw parallel lines, with segments (subnets) coloured green,
> yellow or red depending on the conflicts between the networks.
>
> What would be the simplest/fastest way of getting this done ?
> (the graphic parts, the IP stuff I know how to handle)

AFAIK, probably Matplotlib.  I'm not sure what
quite to imagine for your plot but below is code
that makes an attempt at (maybe) something like
you are describing, shown here:

http://www.flickr.com/photos/67254913@N07/6123112552/in/photostream#/

There are smarter ways to do this in matplotlib, but
this is pretty quick and dirty.  I'm just plotting
lines over-top other lines.  The pic I put is the
plot as a .png, which matplotlib makes if you want,
but you can also view it in a frame that matplotlib
generates and then pan/zoom/etc...


from pylab import *

x = [1,2,3,4,5,6,7,8]
y = [2 for num in x]

#plot the parallel lines themselves in green
for num in range(6):
    y = [num for item in x]
    plot(x,y,color='g',lw='4')

#plot any conflict sections in red or yellow
#some hard data to give the idea:
x2 = [3,4]
y2 = [2 for num in x2]
x3 = [5,6,7]
y3 = [4 for num in x3]
x4 = [2,3]
y4 = [3 for num in x4]

#plot these three colored parts over the green lines
plot(x2,y2,color='r',lw='12')
plot(x3,y3,color='yellow',lw='12')
plot(x4,y4,color='r',lw='12')

pos = arange(6)
yticks(pos, ('net1', 'net2', 'net3', 'net4', 'net5', 'net6'))

show()

#-------------------------

Che



More information about the Python-list mailing list