<jupyter_start><jupyter_text>Visualising data using matplotlibIn this notebook you are going to see how to generate some of the plots we've seen in the lecture. The documentation of matplotlib is really good, so worth having a look if you get stuck.This notebook covers the following examples:* Example 1: Quarterly gas measurements, UK 1960-1986* Example 1: Improving the earlier plot and splitting it by season* Example 2: Visualising disributions * Example 3: Bar chart<jupyter_code>import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline
plt.rc('figure', figsize=(8.0, 4.0), dpi=180)

gas = np.loadtxt("data/gas.csv", delimiter=',')
plt.plot(gas[:, 1], gas[:, 2], "o-") #plt.scatter is another option here but we can't draw a line graph with it
plt.ylabel("Gas consumed (megatherms)")
plt.grid("on") #sometimes keeping the grid on makes the oplot more readable 
plt.ylim(0, 1500)
plt.title("UK Gas quarterly gas usage 1960-1986")
# split into quarterly lines
q1 = gas[::4, :]
q2 = gas[1::4, :]
q3 = gas[2::4, :]
q4 = gas[3::4, :]

#markers and colours are useful here to help us distinguish between the different seasons

plt.plot(q1[:,1], q1[:,2], label='Winter', marker='d') 
plt.plot(q1[:,1], q2[:,2], label='Spring', marker='o')
plt.plot(q1[:,1], q3[:,2], label='Summer', marker='*')
plt.plot(q1[:,1], q4[:,2], label='Autumn', marker='^')

plt.xlabel('Date (years AD)')
plt.ylabel('Gas consumed (megatherms)')
plt.grid("on")
plt.legend()
plt.ylim(0,1500)
plt.title("UK Gas quarterly gas usage 1960-1986")<jupyter_output>/Users/alexpancheva/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/deprecation.py:107: MatplotlibDeprecationWarning: Passing one of 'on', 'true', 'off', 'false' as a boolean is deprecated; use an actual boolean (True/False) instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)<jupyter_text>Your turnKnowing 1 megatherm = 5397 tonnes of CO2, plot year vs total usage of CO2Hint 1: You need to convert the consummed gas into tonnes of CO2Hint 2: You might find np.cumsum useful Hint 3: Don't forget title, labels, etcWhat observations can you make about this plot?  Histograms* Shows count of values within a range. * Used to visualise distributions* Number of bins is important<jupyter_code>x = gas[:,2]
plt.hist(x, bins=np.linspace(0,1200,20));
plt.xlabel("Megatherms")
plt.ylabel("Count of quarters")<jupyter_output><empty_output><jupyter_text>Your turnTo explore how the number of bins can affect your histogram:* Create more bins + visualise the histogram with the gas data* Crater fewer bins + visualise the histogram with the gas data* Place the bins between -500 and 300  Bar chartsWhile they might look a bit like histograms, there are some main differences: * In histograms each column represents a group defined by a continuous variable * In bar charts, each column is a group defined by a categorical variable* A histogram describes a distribution* Order of categorical variables on x axis doesn't matter in a bar chart<jupyter_code>dino = ['T-rex','V-raptor','Brachiosaurus']
y_pos = np.arange(len(dino))
arms = [4,7,12]
 
plt.bar(y_pos,arms,color='green')

plt.xticks(y_pos, dino)
plt.ylabel("Arms length")
plt.title("Length of Dinosaur Arms")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12124002455494168, 'fraction_numerical': 0.03376304481276857, 'mean_word_length': 5.789583333333334, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '158364', 'license': 'CC0-1.0', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1041, 'n_words': 386, 'path': 'day2.ipynb', 'repo_name': 'jeremysinger/glasgow-summer-school', 'size': '3258'}