Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.65 KB

File metadata and controls

60 lines (47 loc) · 1.65 KB

Matplotlib - Solutions

Bar Chart

import numpy as np
x = np.arange(1, 13)
y = np.array([7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8])

from matplotlib import pyplot as plt
plt.bar(x, y, width=0.25, color='green', edgecolor='none')
plt.xlim(1, 13)
plt.ylim(0, 10)
plt.xlabel('month of the year')
plt.ylabel('letters in month name')
plt.title('Leters in the Name of Each Month')
plt.show()

Problem 1 Solution 1

Scatter and Line Plots

import numpy as np
x = np.arange(0.0, 2 * np.pi, np.pi / 16.0)
y = 2 * np.cos(x)

from matplotlib import pyplot as plt
plt.plot(x, y, linewidth=3, c='green', linestyle='--', label='approx')
plt.scatter(x, y, s=300, c='blue', edgecolor='none', label='approx')
plt.xlabel('x')
plt.ylabel('y')
plt.title('2 * cos(x)')
plt.savefig('cos.png')

Problem 1 Solution 2

Multiplot, Histogram, and Errorbar

import numpy as np
mu = 10500
sigma = 250
values = mu + sigma * np.random.randn(10000)
x = mu + sigma * np.random.randn(100)
y = mu + sigma * np.random.randn(100)

from matplotlib import pyplot as plt
plt.subplot(1, 2, 1)
plt.hist(values, 25, facecolor='green', edgecolor='none')
plt.ylabel('count')
plt.subplot(1, 2, 2)
plt.scatter(x, y, s=300, alpha=0.25, edgecolor='none', c=random.rand(100))
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(10000, 11000)
plt.ylim(10000, 11000)
plt.grid()
plt.show()

Problem 1 Solution 3

Back to Problem Set