In [241]:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import math
import pylab
from matplotlib.pyplot import *
from numpy import *
def line_chart(arr):
# Plot a simple line chart based on values from an array
plt.plot(arr)
plt.show()
def line(a, b, c):
# Create a grid from -20 to 20 with spacing of 1
x = np.arange(-20, 20, 1)
# Calculate values for y
y = x**a + b*x + c
plt.plot(x, y)
plt.show()
def quadratic(a, b, c):
# Solve roots for quadratic equation
root = math.sqrt((b * b) - 4 * a * c)
x1 = (-b + root) / (2 * a)
x2 = (-b - root) / (2 * a)
# Return an array of values
return x1, x2
def budget(income, outcome):
# Define months
months = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
# Calculate EBIT
i = 0
ebit = []
while i < len(months):
ebit.append(income[i] - outcome[i])
i += 1
# Define x locations for groups and bar width
ind = np.arange(len(months))
width = 0.4
# Create chart
fig, ax = plt.subplots()
# Define bar data
income_bar = ax.bar(ind, income, width, color = 'r')
outcome_bar = ax.bar(ind + width, outcome, width, color = 'y')
# Add labels and widths
ax.set_ylabel('Euros')
ax.set_title('Budget')
ax.set_xticks(ind+width)
ax.set_xticklabels(months)
# Create line for EBIT
ebit = ax.plot(ax.get_xticks(), ebit, linestyle = '-', marker = 'o', linewidth = 2.0)
# Add legend
ax.legend((income_bar[0], outcome_bar[0], ebit[0]), ('Income', 'Outcome', 'EBIT'), loc='best')
plt.show()
def circle(rad):
# Calculate area and circumference
area = math.pi * rad ** 2
circumference = 2 * math.pi * rad
plt.axes()
# Plot the circle
circle = plt.Circle((0, 0), radius = rad, fc = 'y', alpha = 0.5)
plt.gca().add_patch(circle)
# Add circle in the middle
middle_circle = plt.Circle((0, 0), radius = 0.20, fc = 'r', alpha = 1)
plt.gca().add_patch(middle_circle)
# Automatically scale the axis to fit the circle
plt.axis('scaled')
# Add a line to represent radius
line = plt.Line2D((0, rad), (0, 0), lw=2.5, alpha = 0.5, label = "r = %f" % rad)
plt.gca().add_line(line)
plt.legend()
plt.show()
return area, circumference
#line(2, 5, 2)
#line_chart(np.array([1, 8.5, -9, 21, 22, 33.8, 48.2, 47.2, -45, 42, 12]))
#print(quadratic(2, -8, 2))
# Create arrays for bar chart representing monthly budget
income = np.array([2700, 2100, 2800, 2900, 3100, 3200, 3900, 2100, 2300, 1800, 1700, 6000])
outcome = np.array([2800, 3000, 2400, 2000, 1800, 2800, 4500, 850, 1100, 2000, 2100, 4500])
budget(income, outcome)
#print(circle(10.5))