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

CM cmpython at gmail.com
Wed Sep 7 02:00:58 EDT 2011


On Sep 6, 2:27 pm, Fred Pacquier <xne... at fredp.lautre.net> wrote:
> Hi,
>
> I'm a Python long-timer, but I've never had to use tools like Matplotlib &
> others before.
>
> 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)


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

One fairly simple way is with Matplotlib.  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 fancier 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... This is using the
pylab module, but you could also embed your plots in
a GUI if you prefer.


from pylab import *

#plot the parallel lines in green
x = [1,2,3,4,5,6,7,8]
y = [2 for num in x]
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 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 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')

#change y labels to meaningful labels
pos = arange(6)
yticks(pos, ('net1', 'net2', 'net3', 'net4', 'net5', 'net6'))

show()

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

Che



More information about the Python-list mailing list