Python For Quantum Mechanics
#

Cheatsheet: Week 5
#

Making a Plot#

Formatting#

Figure Object#

plt.

ax.

xlim

set_xlim

ylim

set_ylim

xticks

set_xticks

yticks

set_yticks

title

set_title

xlabel

set_xlabel

ylabel

set_ylabel

  • Saving an image

    • Save fig as first_image.png

    • fig.savefig('first_image.png')

Multiple Plots#

Other Plots#

3D Plots#

5.7 Animations#

  • Changing to an interactive backend

    • %matplotlib notebook makes interactive plots

    • %matplotlib inline is default and makes noninteractive plots

  • Making an initialisation function

    • An empty plot is first performend using line, = ax.plot([], [])

    • An function init() function which will change the data plotted to x and y using line.set_data(x,y) and will return line,.

  • Making an animation function

  • Animating a plot

    • Animation requires us to import a function.

      • from matplotlib.animation import FuncAnimation

    • To animate fig with my_frame_num number of frames, a interval of my_inter milliseconds between frames, and a repeat delay of my_delay milliseconds.

    • anim = FuncAnimation(fig, animate, init_func=init, frames=my_frame_num, interval=my_inter, repeat_delay=my_delay, blit=True)

    • Documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html

  • Saving an animation

Tutorial 5#

  1. Noise in qubits

  2. Particle in a Box

  3. Normal Distribution

  4. Monte Carlo \(\pi\) Estimation

  5. Ball Animation

Exercises 5#

  1. Settlers of Histogram

  2. Normal Distribution

  3. Particle in a Box

  4. Plotting a Sphere

  5. Snake Animation