Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 801faf5

Browse files
committed
updates to the FAQ
svn path=/trunk/matplotlib/; revision=6242
1 parent 39dd57a commit 801faf5

2 files changed

Lines changed: 139 additions & 14 deletions

File tree

doc/faq/howto_faq.rst

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,4 +470,127 @@ a movie, and then cleans up::
470470

471471
.. htmlonly::
472472

473-
See :ref:`animation-movie_demo` for a complete example.
473+
Josh Lifton provided this example :ref:`animation-movie_demo`, which is possibly dated since it was written in 2004.
474+
475+
476+
.. _howto-twoscale:
477+
478+
Can I have multiple y-axis scales?
479+
==================================
480+
481+
A frequent request is to have two scales for the left and right
482+
y-axis, which is possible using :func:`~matplotlib.pyplot.twinx` (more
483+
than two scales are not currently supported, though it is on the wishq
484+
list). This works pretty well, though there are some quirks when you
485+
are trying to interactively pan and zoom, since both scales do not get
486+
the signals.
487+
488+
The approach :func:`~matplotlib.pyplot.twinx` (and its sister
489+
:func:`~matplotlib.pyplot.twiny`) uses is to use *2 different axes*,
490+
turning the axes rectangular frame off on the 2nd axes to keep it from
491+
obscuring the first, and manually setting the tick locs and labels as
492+
desired. You can use separate matplotlib.ticker formatters and
493+
locators as desired since the two axes are independent::
494+
495+
import numpy as np
496+
import matplotlib.pyplot as plt
497+
498+
fig = plt.figure()
499+
ax1 = fig.add_subplot(111)
500+
t = np.arange(0.01, 10.0, 0.01)
501+
s1 = np.exp(t)
502+
ax1.plot(t, s1, 'b-')
503+
ax1.set_xlabel('time (s)')
504+
ax1.set_ylabel('exp')
505+
506+
ax2 = ax1.twinx()
507+
s2 = np.sin(2*np.pi*t)
508+
ax2.plot(t, s2, 'r.')
509+
ax2.set_ylabel('sin')
510+
plt.show()
511+
512+
513+
.. htmlonly::
514+
515+
See :ref:`api-two_scales` for a complete example
516+
517+
.. _howto-batchmode:
518+
519+
Can I just generate images without having a window popup?
520+
=====================================================
521+
522+
The easiest way to do this is use an image backend (see
523+
:ref:`what-is-a-backend`) such as Agg (for PNGs), PDF, SVG or PS. In
524+
your figure generating script, just place call
525+
:func:`matplotlib.use` directive before importing pylab or
526+
pyplot::
527+
528+
import matplotlib
529+
matplotlib.use('Agg')
530+
import matplotlib.pyplot as plt
531+
plt.plot([1,2,3])
532+
plt.savefig('myfig')
533+
534+
535+
.. seealso::
536+
:ref:`howto-webapp`
537+
538+
('SHOW',
539+
"What's up with 'show'? Do I have to use it?",
540+
"""
541+
542+
.. _howto-show
543+
544+
How should I use :func:`~matplotlib.pyplot.show`?
545+
=================================================
546+
547+
The user interface backends need to start the GUI mainloop, and this
548+
is what :func:`~matplotlib.pyplot.show` does. It tells matplotlib to
549+
raise all of the figure windows and start the mainloop. Because the
550+
mainloop is blocking, you should only call this once per script, at
551+
the end. If you are using matplotlib to generate images only and do
552+
not want a user interface window, you can skip it (see
553+
:ref:`howto-batch` and :ref:`what-is-a-backend`).
554+
555+
556+
Because it is expensive to draw, matplotlib does not want to redrawing the figure
557+
many times in a script such as the following::
558+
559+
plot([1,2,3]) # draw here ?
560+
xlabel('time') # and here ?
561+
ylabel('volts') # and here ?
562+
title('a simple plot') # and here ?
563+
show()
564+
565+
566+
It is *possible* to force matplotlib to draw after every command,
567+
which is what you usually want when working interactively at the
568+
python console, but in a script you want to defer all drawing until
569+
the script has executed (see :ref:`mpl-shell`). This is especially
570+
important for complex figures that take some time to draw.
571+
:func:`~matplotlib.pyplot.show` is designed to tell matplotlib that
572+
you're all done issuing commands and you want to draw the figure now.
573+
574+
.. note::
575+
576+
:func:`~matplotlib.pyplot.show` should be called at most once per
577+
script and it should be the last line of your script. At that
578+
point, the GUI takes control of the interpreter. If you want to
579+
force a figure draw, use :func:`~matplotlib.pyplot.draw` instead.
580+
581+
Many users are frustrated by show because they want it to be a
582+
blocking call that raises the figure, pauses the script until the
583+
figure is closed, and then allows the script to continue running until
584+
the next figure is created and the next show is made. Something like
585+
this::
586+
587+
# WARNING : illustrating how NOT to use show
588+
for i in range(10):
589+
# make figure i
590+
show()
591+
592+
This is not what show does and unfortunately, because doing blocking
593+
calls across user interfaces can be tricky, is currently unsupported,
594+
though we have made some pregress towards supporting blocking events.
595+
596+

examples/pylab_examples/two_scales.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@
1919
2020
"""
2121

22-
from pylab import *
22+
import numpy as np
23+
import matplotlib.pyplot as plt
2324

24-
ax1 = subplot(111)
25-
t = arange(0.01, 10.0, 0.01)
26-
s1 = exp(t)
27-
plot(t, s1, 'b-')
28-
xlabel('time (s)')
29-
ylabel('exp')
25+
fig = plt.figure()
26+
ax1 = fig.add_subplot(111)
27+
t = np.arange(0.01, 10.0, 0.01)
28+
s1 = np.exp(t)
29+
ax1.plot(t, s1, 'b-')
30+
ax1.set_xlabel('time (s)')
31+
ax1.set_ylabel('exp')
3032

3133

3234
# turn off the 2nd axes rectangle with frameon kwarg
33-
ax2 = twinx()
34-
s2 = sin(2*pi*t)
35-
plot(t, s2, 'r.')
36-
ylabel('sin')
37-
ax2.yaxis.tick_right()
38-
show()
35+
ax2 = ax1.twinx()
36+
s2 = np.sin(2*np.pi*t)
37+
ax2.plot(t, s2, 'r.')
38+
ax2.set_ylabel('sin')
39+
plt.show()
40+

0 commit comments

Comments
 (0)