程序代写 COMP2420/COMP6420 – Introduction to Data Management, Analysis and Security

Basic Visualisation (Sols)

COMP2420/COMP6420 – Introduction to Data Management, Analysis and Security
Lecture – Basic Visualisation

Copyright By PowCoder代写 加微信 powcoder

Matplotlib – Line Plots¶

from matplotlib import pyplot as plt
%matplotlib inline

1. Plot the Line¶

time = [0, 1, 2, 3, 4]
revenue = [200, 400, 650, 800, 850]
costs = [150, 500, 550, 550, 560]
plt.plot(time, revenue)
plt.plot(time, costs)
plt.show()

2. Changing the appearance of the line¶

plt.plot(time, revenue, color=’purple’, linestyle=’–‘)
plt.plot(time, costs, color=’#82edc9′, marker=’s’)
plt.show()

3. Zooming in on different parts of the axis¶

plt.plot(time, costs)
plt.axis([1, 4, 500, 560])
plt.show()

4. Putting labels on titles and axes¶

x = range(12)
y = [3000, 3005, 3010, 2900, 2950, 3050, 3000, 3100, 2980, 2980, 2920, 3010]
plt.plot(x, y)
plt.axis([0, 11, 2900, 3100])

plt.xlabel(‘Time’)
plt.ylabel(‘Dollars spent on coffee’)
plt.title(‘My Last Twelve Years of Coffee Drinking’)
plt.show()

5. Creating a more complex figure layout¶

x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
z = [3, 2, 1, 1]

plt.subplot(1, 2, 1)
plt.plot(x, y, color=’green’)
plt.ylabel(‘Y Label 1’)
plt.title(‘First Subplot’)

plt.subplot(1, 2, 2)
plt.plot(x, z, color=’steelblue’)
plt.ylabel(‘Y Label 2’)
plt.title(‘Second Subplot’)
plt.subplots_adjust(wspace=0.4)
plt.show()

6. Arranging Sub-Plots¶

x = range(7)
straight_line = [0, 1, 2, 3, 4, 5, 6]
parabola = [0, 1, 4, 9, 16, 25, 36]
cubic = [0, 1, 8, 27, 64, 125, 216]

plt.subplot(2, 1, 1)
plt.plot(x, straight_line)

plt.subplot(2, 2, 3)
plt.plot(x, parabola)

plt.subplot(2, 2, 4)
plt.plot(x, cubic)

plt.subplots_adjust(wspace=0.35, bottom=0.2)
plt.show()

6. Adding legends to graphs¶

months = range(12)
hyrule = [63, 65, 68, 70, 72, 72, 73, 74, 71, 70, 68, 64]
kakariko = [52, 52, 53, 68, 73, 74, 74, 76, 71, 62, 58, 54]
gerudo = [98, 99, 99, 100, 99, 100, 98, 101, 101, 97, 98, 99]

plt.plot(months, hyrule)
plt.plot(months, kakariko)
plt.plot(months, gerudo)

# Alternative
## plt.plot(months, hyrule, legend=’hyrule’)
## plt.legend()

legend_labels = [‘Hyrule’, ‘Kakariko’, ‘Gerudo Valley’]
plt.legend(legend_labels)

plt.show()

Matplotlib & Pandas¶

import pandas as pd

df = pd.DataFrame({
‘name’:[‘john’,’mary’,’peter’,’jeff’,’bill’,’lisa’,’jose’],
‘age’:[23,78,22,19,45,33,20],
‘gender’:[‘M’,’F’,’M’,’M’,’M’,’F’,’M’],
‘state’:[‘california’,’dc’,’california’,’dc’,’california’,’texas’,’texas’],
‘num_children’:[2,0,0,3,2,1,4],
‘num_pets’:[5,1,0,5,2,2,3]

name age gender state num_children num_pets
0 john 23 M california 2 5
1 mary 78 F dc 0 1
2 peter 22 M california 0 0
3 jeff 19 M dc 3 5
4 bill 45 M california 2 2
5 lisa 33 F texas 1 2
6 jose 20 M texas 4 3

1. Plot two dataframe columns as a scatter plot¶

plt.scatter(df[‘num_children’], df[‘num_pets’]);

2. Plot column values as a bar plot¶

plt.bar(df[‘name’], df[‘age’]);

3. Line plot with multiple columns¶

plt.plot(df[‘name’], df[‘num_children’], label = ‘Number of Children’)
plt.plot(df[‘name’], df[‘num_pets’], label = ‘Number of Pets’)
plt.legend()
plt.show()

4. Bar plot with group by¶

df.groupby(‘state’)[‘name’].nunique().plot(kind=’bar’)
plt.show()

5. Plot histogram of column values¶

df[[‘age’]].plot(kind=’hist’,bins=[0,20,40,60,80,100],rwidth=0.8)
plt.show()

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com