English 中文(简体)
Couple of matplotlib newbie doubts
原标题:

I am just starting to use matplotlib and I have hit upon 2 major roadblocks, which I can t seem to work around from the docs/examples,etc: Here is Python source:

#!/usr/bin/python
import matplotlib
matplotlib.use( Agg )

import matplotlib.pyplot as plt
for i in range(0,301):

    print "Plotting",i

    # Reading a single column data file
    l=plt.plotfile("gen"+str(i))

    plt.xlabel( Population )
    plt.ylabel( Function Value )
    plt.title( Generation +str(i))
    plt.axis([0,500,0,180])

    plt.plot()

    if len(str(i)) == 1:
        plt.savefig("../images/plot00"+str(i)+".png")
    if len(str(i)) == 2:
        plt.savefig("../images/plot0"+str(i)+".png")
    if len(str(i)) == 3:
        plt.savefig("../images/plot"+str(i)+".png")

    plt.clf()
  1. Doubt 1: As you can see, I am basically clearing the plot and then saving the new plot every time. I want to keep the range of the Y-axis constant and I am trying to do it via "plt.axis([0,500,0,180])". But it doesn;t seem to work and it is automatically set everytime.
  2. Doubt 2: Instead of obtaining the default plot in which the points are joined by continuous lines, I would prefer to obtain a plot of say, * . How would I do that?
最佳回答

  • As Tim Pietzcker points out, you can shorten if filename code at the end by using string number formatting.
    filename= plot%03d.png %i
    

    replaces %03d with the integer i padded with up to 3 zero s. In Python2.6+, one can do the same thing with the less pretty but more powerful new string formatting syntax:

    filename= plot{0:03d}.png .format(i)
    

  • To get the points plotted with stars, you can use the option marker= * . And to get rid of the connecting lines, use linestyle= none .
  • plt.plotfile(...) plots a figure. The call to plt.plot() plots a second figure overlaid on top of the first figure. The call to plt.plot() seems to modify the axis dimensions, wiping out the effect of plt.axis(...). Fortunately, the fix is simple: simply don t call plt.plot(). You don t need it.
#!/usr/bin/env python
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use( Agg )   # This can also be set in ~/.matplotlib/matplotlibrc
for i in range(0,3):
    print  Plotting ,i
    # Reading a single column data file
    plt.plotfile( gen%s %i,linestyle= none , marker= * )

    plt.xlabel( Population )
    plt.ylabel( Function Value )
    plt.title( Generation%s %i)
    plt.axis([0,500,0,180])
    # This (old-style string formatting) also works, especial for Python versions <2.6:
    # filename= plot%03d.png %i
    filename= plot{0:03d}.png .format(i)
    print(filename)
    plt.savefig(filename)
    # plt.clf()  # clear current figure
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签