## Re: [Matplotlib-users] starting with pplots

 Re: [Matplotlib-users] starting with pplots From: Goyo - 2010-12-23 02:05:54 2010/12/19 Pawel : > Hi, > > I am a new user of matplotlib so maybe my question is elementary, but > have not been able to find an answer to my problem in the archive. > > I would like to make a 2D plot of colored points of 3D data (clusters). > My data looks like this: > > 11837.2120    -0.0858    2.0000 > 23975.2120    -0.0672    2.0000 > 37609.2120    -0.0306    2.0000 > 53263.9800    -0.0690    2.0000 > 72106.6760    0.2708     1.0000 > 92674.6760    -0.0129    3.0000 > 116758.676    -0.1245    3.0000 > ... > > So I need to plot the first and second column as points on the x-y axis > and color the points according to the numbers in the third column (which > are integers ranging from 1 to5). > > I'd appreciate any help. I realize something so typical should be > somewhere in the documentation but I was not able to find it. Try this: import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap x, y, z = np.loadtxt('data.txt', unpack=True) cmap = ListedColormap(['b', 'g', 'r', 'c', 'm']) plt.scatter(x, y, c=z, cmap=cmap, vmin=1, vmax=5) plt.show() You'll need to use a single space as column delimiter in your data file or deal with more loadtxt arguments. If your z data were color specifications you could just use plt.scatter(x, y, c=z) as stated in the scatter docstring. Converting arbitrary data to color specifications is the non trivial issue here. You can write your own code to do this or use colormaps. Goyo

### Thread view

 [Matplotlib-users] starting with pplots From: Pawel - 2010-12-19 01:05:06 Hi, I am a new user of matplotlib so maybe my question is elementary, but have not been able to find an answer to my problem in the archive. I would like to make a 2D plot of colored points of 3D data (clusters). My data looks like this: 11837.2120 -0.0858 2.0000 23975.2120 -0.0672 2.0000 37609.2120 -0.0306 2.0000 53263.9800 -0.0690 2.0000 72106.6760 0.2708 1.0000 92674.6760 -0.0129 3.0000 116758.676 -0.1245 3.0000 ... So I need to plot the first and second column as points on the x-y axis and color the points according to the numbers in the third column (which are integers ranging from 1 to5). I'd appreciate any help. I realize something so typical should be somewhere in the documentation but I was not able to find it. Thanks, Paul
 Re: [Matplotlib-users] starting with pplots From: Goyo - 2010-12-23 02:05:54 2010/12/19 Pawel : > Hi, > > I am a new user of matplotlib so maybe my question is elementary, but > have not been able to find an answer to my problem in the archive. > > I would like to make a 2D plot of colored points of 3D data (clusters). > My data looks like this: > > 11837.2120    -0.0858    2.0000 > 23975.2120    -0.0672    2.0000 > 37609.2120    -0.0306    2.0000 > 53263.9800    -0.0690    2.0000 > 72106.6760    0.2708     1.0000 > 92674.6760    -0.0129    3.0000 > 116758.676    -0.1245    3.0000 > ... > > So I need to plot the first and second column as points on the x-y axis > and color the points according to the numbers in the third column (which > are integers ranging from 1 to5). > > I'd appreciate any help. I realize something so typical should be > somewhere in the documentation but I was not able to find it. Try this: import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap x, y, z = np.loadtxt('data.txt', unpack=True) cmap = ListedColormap(['b', 'g', 'r', 'c', 'm']) plt.scatter(x, y, c=z, cmap=cmap, vmin=1, vmax=5) plt.show() You'll need to use a single space as column delimiter in your data file or deal with more loadtxt arguments. If your z data were color specifications you could just use plt.scatter(x, y, c=z) as stated in the scatter docstring. Converting arbitrary data to color specifications is the non trivial issue here. You can write your own code to do this or use colormaps. Goyo
 Re: [Matplotlib-users] starting with pplots From: Paul Ivanov - 2010-12-23 02:27:25 Attachments: Message as HTML Pawel, on 2010-12-18 20:04, wrote: > Hi, > > I am a new user of matplotlib so maybe my question is elementary, but > have not been able to find an answer to my problem in the archive. > > I would like to make a 2D plot of colored points of 3D data (clusters). > My data looks like this: > > 11837.2120 -0.0858 2.0000 > 23975.2120 -0.0672 2.0000 > 37609.2120 -0.0306 2.0000 > 53263.9800 -0.0690 2.0000 > 72106.6760 0.2708 1.0000 > 92674.6760 -0.0129 3.0000 > 116758.676 -0.1245 3.0000 > ... > > So I need to plot the first and second column as points on the x-y axis > and color the points according to the numbers in the third column (which > are integers ranging from 1 to5). > > I'd appreciate any help. I realize something so typical should be > somewhere in the documentation but I was not able to find it. Hi Paul, welcome to matplotlib! So you need to read in those columns somehow (as numpy arrays, or lists), but once you've got that, it's just a matter of initiating a 3d plot, and calling scatter with the three columns. take a look at this example and it's source code: http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html#scatter-plots for your purposes, the code will be something like: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x,y,z) best, -- Paul Ivanov 314 address only used for lists, off-list direct email at: http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7
 Re: [Matplotlib-users] ODP: starting with pplots From: Paul Ivanov - 2010-12-23 20:02:47 Attachments: Message as HTML Pawel Janowski, on 2010-12-23 10:09, wrote: > Hi Pavel, > > Thanks for your help. Matplotlib seems to be a really cool tool. Your > response almost answered my question. What I want is for the 3D plot to be > 2D. I mean the z-axis can only take on 5 discreet values so I don't want to > visualize 3 dimensions but just two with the data points colored five > different colors depending on the z value. Pawel, (I'm replying back to the list, so that others may benefit - hello there, search engine visitors from the future!) In that case, you can either follow Goya's suggestion - if you only want to draw points. scatter will actually rescale the color values you give to whatever colormap you're using - and for your case, with just five z values in range(1,6), I found a slight tweak to the 'hsv' colormap does the trick. from numpy.random import rand, randint import matplotlib.pyplot as plt x,y = rand(2,100) z = randint(1,6,100) plt.scatter(x,y,c=z, vmin=-1, cmap=plt.get_cmap('hsv')) You can see the built-in colormaps here: http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html and as Goyo showed, it's pretty easy to make a new one. If you want more control, such as changing the shape of the marker, not just the color, or if there's some order to your points that you want to also see (for example, draw lines between points of the same z value) - you can use a boolean mask. from numpy.random import rand, randint import matplotlib.pyplot as plt x,y = rand(2,100) z = randint(1,6,100) for i,c,m in zip(range(1,6),'rgbmk', 'odp*s'): mask = z==i plt.plot(x[mask],y[mask], color=c, marker=m) hope that helps, -- Paul Ivanov 314 address only used for lists, off-list direct email at: http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7
 [Matplotlib-users] ODP: ODP: starting with pplots From: Pawel Janowski - 2010-12-23 20:23:16 Hey guys, Thank you so much for your clear answers which have been very helpful! Pawel -----Wiadomość oryginalna----- Od: Paul Ivanov [mailto:pivanov314@...] Wysłano: Thursday, December 23, 2010 3:03 PM Do: matplotlib-users@... DW: Pawel Janowski Temat: Re: ODP: [Matplotlib-users] starting with pplots Pawel Janowski, on 2010-12-23 10:09, wrote: > Hi Pavel, > > Thanks for your help. Matplotlib seems to be a really cool tool. Your > response almost answered my question. What I want is for the 3D plot > to be 2D. I mean the z-axis can only take on 5 discreet values so I > don't want to visualize 3 dimensions but just two with the data points > colored five different colors depending on the z value. Pawel, (I'm replying back to the list, so that others may benefit - hello there, search engine visitors from the future!) In that case, you can either follow Goya's suggestion - if you only want to draw points. scatter will actually rescale the color values you give to whatever colormap you're using - and for your case, with just five z values in range(1,6), I found a slight tweak to the 'hsv' colormap does the trick. from numpy.random import rand, randint import matplotlib.pyplot as plt x,y = rand(2,100) z = randint(1,6,100) plt.scatter(x,y,c=z, vmin=-1, cmap=plt.get_cmap('hsv')) You can see the built-in colormaps here: http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.htm l and as Goyo showed, it's pretty easy to make a new one. If you want more control, such as changing the shape of the marker, not just the color, or if there's some order to your points that you want to also see (for example, draw lines between points of the same z value) - you can use a boolean mask. from numpy.random import rand, randint import matplotlib.pyplot as plt x,y = rand(2,100) z = randint(1,6,100) for i,c,m in zip(range(1,6),'rgbmk', 'odp*s'): mask = z==i plt.plot(x[mask],y[mask], color=c, marker=m) hope that helps, -- Paul Ivanov 314 address only used for lists, off-list direct email at: http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7