Plotting with Matplotlib

What is Matplotlib

It’s a graphing library for Python. It has a nice collection of tools that you can use to create anything from simple graphs, to scatter plots, to 3D graphs. It is used heavily in the scientific Python community for data visualization.

Let’s plot a simple sin wave

1import matplotlib.pyplot as plt
2import numpy as np
3
4x = np.linspace(0, 2*np.pi, 50)
5plt.plot(x, np.sin(x))
6plt.show() # we can't do this on our VM server
7plt.savefig('my_sinwave.png')

we can keep plotting! Let’s plot 2 graphs on the same axis

1plt.plot(x, np.sin(x), np.sin(2*x))
2plt.show()
3plt.savefig('my_sinwavex2.png')

why stop now? Let’s make the plot easier to read

1plt.plot(x, np.sin(x), 'r-o', x, np.sin(2*x), 'g--'
2plt.show()
3plt.savefig('my_sinwavex2a.png')

other color combinations:

Colors:

  • Blue – ‘b’

  • Green – ‘g’

  • Red – ‘r’

  • Cyan – ‘c’

  • Magenta – ‘m’

  • Yellow – ‘y’

  • Black – ‘k’ (‘b’ is taken by blue so the last letter is used)

  • White – ‘w’

Lines and markers:

  • Lines:

    • Solid Line – ‘-’

    • Dashed – ‘–’

    • Dotted – ‘.’

    • Dash-dotted – ‘-:’

  • Often Used Markers:

    • Point – ‘.’

    • Pixel – ‘,’

    • Circle – ‘o’

    • Square – ‘s’

    • Triangle – ‘^’

Subplots

using the subplot() function, we can plot two graphs at the same time within the same “canvas”. Think of the subplots as “tables”, each subplot is set with the number of rows, the number of columns, and the active area, the active areas are numbered left to right, then up to down.

1plt.subplot(2, 1, 1) # (row, column, active area)
2plt.plot(x, np.sin(x))
3plt.subplot(2, 1, 2) # switch the active area
4plt.plot(x, np.sin(2*x))
5plt.show()
6plt.savefig('my_sinwavex2b.png')

Scatter plots

1y = np.sin(x)
2plt.scatter(x,y)
3plt.show()
4plt.savefig('my_scattersin.png')

Let’s mix things up, using random numbers and add a colormap to a scatter plot

1x = np.random.rand(1000)
2y = np.random.rand(1000)
3size = np.random.rand(1000) * 50
4color = np.random.rand(1000)
5plt.scatter(x, y, size, color)
6plt.colorbar()
7plt.show()
8plt.savefig('my_scatterrandom.png')

We brought in two new parameters, size and color, which will vary the diameter and the color of our points. Then adding the colorbar() gives us a nice color legend to the side.

Histograms

A histogram is one of the simplest types of graphs to plot in Matplotlib. All you need to do is pass the hist() function an array of data. The second argument specifies the amount of bins to use. Bins are intervals of values that our data will fall into. The more bins, the more bars.

1plt.hist(x, 50)
2plt.show()
3plt.savefig('my_histrandom.png')

Adding Labels and Legends

1x = np.linspace(0, 2 * np.pi, 50)
2plt.plot(x, np.sin(x), 'r-x', label='Sin(x)')
3plt.plot(x, np.cos(x), 'g-^', label='Cos(x)')
4plt.legend() # Display the legend.
5plt.xlabel('Rads') # Add a label to the x-axis.
6plt.ylabel('Amplitude') # Add a label to the y-axis.
7plt.title('Sin and Cos Waves') # Add a graph title.
8plt.show()
9plt.savefig('my_labels_legends')

Redis and plots

you can “save” your plots to Redis, however the maximum size for a key/value is 512 mb and the sum of all your data (including files) must fit into main memory on the Redis server.

1import redis
2rd = redis.StrictRedis(host='172.17.0.1', port=6379, db=0)
3
4# read the raw file bytes into a python object
5file_bytes = open('/tmp/myfile.png', 'rb').read()
6
7# set the file bytes as a key in Redis
8rd.set('key', file_bytes)