Docsity

Python Matplotlib, Schemes and Mind Maps of Advanced Computer Programming

Partial preview of the text

Download Python Matplotlib and more Schemes and Mind Maps Advanced Computer Programming in PDF only on Docsity!

Python Matplotlib

Han-­‐Wei Shen

The Ohio State University

First Thing First

• Install the Matplotlib package if you have not

had it already

– Typically Matplotlib is installed together with

numpy and scipy

– Example: Use Anaconda Python

(hHps://store.conKnuum.io/cshop/anaconda/)

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a numbr, a string, etc. as the input plt.plot(np.array[4,6,3,10,7]) // use line graph here plt.xlabel(‘input parameter (seconds)’ // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a number, a string, etc. as the input plt.plot(np.array[4,6,3,10,7]) // use line graph here plt.xlabel(‘input parameter (seconds)’ // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a numbr, a string, etc. as the input plt.plot(np.array[4,6,3,10,7]) // use line graph here plt.xlabel(‘input parameter (seconds)’ // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a numbr, a string, etc. as the input plt.xlabel(‘input parameter (seconds)’ ) // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file plt.plot(np.array([4,6,3,10,7])) // use line graph here

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a numbr, a string, etc. as the input plt.plot(np.array([4,6,3,10,7])) // use line graph here plt.xlabel(‘input parameter (seconds)’ // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file

A Typical Matplotlib Program

  • Start your Python program by including matplotlib and numpy packages

(numpy arrays are typically used as the input to matplotlib )

  • Create a figure as the drawing area. You can have mulKple figures in a

program

  • Plot the data using a plot type of your choice (line graph, bar chart,

histogram, pie chart, etc)

  • Add Ktle, labels, Kckmarks, legends, etc.
  • Display the plot either to an interacKve window or a file import matplotlib.pyplot as plt import numpy as np plt.figure() // default argument is empty, but you use a numbr, a string, etc. as the input plt.plot(np.array([4,6,3,10,7])) // use line graph here plt.xlabel(‘input parameter (seconds)’ // label the x axis plt.show() // draw to a window plt.savefig(‘myplot.png’) // save to a file

Display MulKple Plots

• Use ‘subplot()’ command to display mulKple plots together

plt.subplot(211) // create a 2x1 grid for subplots, and you will draw on the first subplot plt.plot(np.array([4,6,3,10,7]) // use line graph here plt.subplot(212) plt.plot(np.random.uniform(10,15,20) plt.show()

Display MulKple Plots

• Use ‘subplot()’ command to display mulKple plots together

plt.subplot(211) // create a 2x1 grid for subplots, and you will draw on the first subplot plt.plot(np.array([4,6,3,10,7]) // use line graph here plt.subplot(212) plt.plot(np.random.uniform(10,15,20) plt.show()

Tick Marks, Labels, and Grid

• You can customize Kck marks, labels, and grid

plt.plot(np.random.uniform(10,15,20)) plt.xKcks(np.arange(1,20)) plt.ylabel(‘random’) plt.grid() plt.show()

Tick Marks, Labels, and Grid

• You can customize Kck marks, labels, and grid

plt.plot(np.random.uniform(10,15,20)) plt.xKcks(np.arange(1,20)) plt.ylabel(‘random’) plt.grid() plt.show()

MulKple Graphs and Legend

• You can put mulKple graphs in a single plot

and add legend

x = np.arang(-­‐2np.pi, 2np.pi, 0.1) plt.plot(x, np.sin(x), label=‘graph1’) plt.plot(x, np.cos(x), label = ‘graph2’) plt.legend() plt.show()

Bar Charts

• Use the funcKon bar(xlist, ylist) to draw a bar

chart

x= [1,2,3,4,5] y = [7,2,3,5.5, 1] plt.xKcks(x,[‘b1’,’b2’,’b3’,’b4’,’b5’],rotaKon=-­‐60) plt.bar(x,y, align=‘center’) plt.grid() plt.show()