On 11/30/2011 1:08 PM stm atoc said...
Hi there,

I have a question regarding plotting with Python.

I have the following python script:

# coding: utf-8
from pylab import *
import numpy

filename='ourtest_out.list'

fh=open(filename)
line=fh.readline()
fh.close

z=array([ float(val) for val in line.split()[1:] ])


a = numpy.loadtxt(filename,skiprows=3)
N=100
t = a[:,0]
nu = a[0:,1:N+1]
#Conc = a[1:,N+1:]
Conc = a[1:,N+1:]

levels=arange(-10,1)
levels=levels[-3]-levels
t=t/360.

figure()
plot(Conc[0],z)

xlabel('C')
ylabel('z')
#show()
savefig('Conc.png')
close()

#########nu
figure()
lw = 2.0 #linewidth
dpi = 96

levels=arange(-10,1)
levels=levels[-3]-levels
plot(nu[0],z)
xlabel('nu')
ylabel('z')
savefig('nu.png')
close()


--------However, once I run the program (run.py)

I have error like this:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/…./run.py in<module>()
      24
      25 figure()
--->  26 plot(Conc[0],z)
      27
      28 xlabel('C')

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/pyplot.py
in plot(*args, **kwargs)
    2284         ax.hold(hold)
    2285     try:
->  2286         ret = ax.plot(*args, **kwargs)
    2287         draw_if_interactive()
    2288     finally:

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py
in plot(self, *args, **kwargs)
    3781         lines = []
    3782
->  3783         for line in self._get_lines(*args, **kwargs):
    3784             self.add_line(line)
    3785             lines.append(line)

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py
in _grab_next_args(self, *args, **kwargs)
     315                 return
     316             if len(remaining)<= 3:
-->  317                 for seg in self._plot_args(remaining, kwargs):
     318                     yield seg
     319                 return

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py
in _plot_args(self, tup, kwargs)
     292             x = np.arange(y.shape[0], dtype=float)
     293
-->  294         x, y = self._xy_from_xy(x, y)
     295
     296         if self.command == 'plot':

/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/matplotlib/axes.py
in _xy_from_xy(self, x, y)
     232         y = np.atleast_1d(y)
     233         if x.shape[0] != y.shape[0]:
-->  234             raise ValueError("x and y must have same first dimension")
     235         if x.ndim>  2 or y.ndim>  2:
     236             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension


-------So, What would you suggest?

Looking over the traceback and code, it would appear the error is saying that there is an inconsistency with the arguments expected vs the arguments passed, which appears in this case to relate to ...

plot(Conc[0],z)

... which derives its parameters from the two lines ...

z=array([ float(val) for val in line.split()[1:] ])

... and ...

a = numpy.loadtxt(filename,skiprows=3)


So, I'd conclude that I'd need a better understanding of how to use the functions plot, array and numpy.loadtext. Neither plot nor array are python builtins nor defined within your script, so they're likely brought in from ...

from pylab import *

... which is generally not something you want to do except when first starting to experiment and learn a new module, and then I'd keep things to the interactive interpreter for testing and discovery. This form of import is generally thought of as polluting the namespace and may allow library specific names to mask python builtins. For example. suppose a module 'xyz' contains a special 'print' function. Executing 'from xyz import *' would shadow the python builtin print function essentially making it inaccessible. It's possible (although unlikely in the case of pylab specifically) that any python builtins that are used in your script have been replaced with pylab versions. A better technique is to simply import pylab and refer to its functions as pylab.xyz so that no ambiguity is possible.

So, read up on pylab, find their support list [1], and follow up there. We focus mainly on getting you far enough along with python basics and generally leave specific library support to the library authors and support groups.

HTH

Emile


[1] start at http://www.scipy.org/Mailing_Lists

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to