Python Matplotlib - 7. Scatter Plots

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Output
  4. CSV data

Run Code 1 in Python

# file: 'starting_code.py'
import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

x = [5, 7, 8, 5, 6, 7, 9, 2, 3, 4, 4, 4, 2, 6, 3, 6, 8, 6, 4, 1]
y = [7, 4, 3, 9, 1, 3, 2, 5, 2, 4, 8, 7, 1, 6, 4, 9, 7, 7, 5, 1]


# colors = [7, 5, 9, 7, 5, 7, 2, 5, 3, 7, 1, 2, 8, 1, 9, 2, 5, 6, 7, 5]

# sizes = [209, 486, 381, 255, 191, 315, 185, 228, 174,
#          538, 239, 394, 399, 153, 273, 293, 436, 501, 397, 539]

# data = pd.read_csv('2019-05-31-data.csv')
# view_count = data['view_count']
# likes = data['likes']
# ratio = data['ratio']

# plt.title('Trending YouTube Videos')
# plt.xlabel('View Count')
# plt.ylabel('Total Likes')

plt.tight_layout()

plt.show()

starting_code.py

Run Code 2 in Python

# file: 'finished_code.py'
import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('2019-05-31-data.csv')
view_count = data['view_count']
likes = data['likes']
ratio = data['ratio']

plt.scatter(view_count, likes, c=ratio, cmap='summer',
            edgecolor='black', linewidth=1, alpha=0.75)

cbar = plt.colorbar()
cbar.set_label('Like/Dislike Ratio')

plt.xscale('log')
plt.yscale('log')

plt.title('Trending YouTube Videos')
plt.xlabel('View Count')
plt.ylabel('Total Likes')

plt.tight_layout()

plt.show()

finished_code.py

Output

CSV data

# file: '2019-05-31-data.csv'
view_count,likes,ratio
8036001,324742,96.91
9378067,562589,98.19
2182066,273650,99.38
6525864,94698,96.25
9481284,582481,97.22
1853121,89903,97.46
2875684,183163,94.52
483827,4864,91.53
1677046,103227,97.52
289756,2387,92.95
2561907,237728,98.8
468390,25346,98.34
18977153,768968,98.73
365731,5997,93.29
680701,41543,97.99
5748289,225966,99.17
3575950,374937,97.69
865788,31806,98.3
5433739,389145,98.84
3643458,369667,97.88
247602,1516,89.18
300443,25429,99.49
313500,56891,98.35
3525217,92948,95.29
195072,23832,98.97
142697,20708,98.91
456783,2625,94.53
601565,38792,98.34
6021472,342044,97.54
940583,14292,97.7
446569,7557,97.15
767900,11091,97.14
5895810,98088,95.87
381910,45178,99.21
2468645,188315,98.73
407859,19407,98.77
846399,29308,95.93
872092,27298,94.85
1279718,98471,99.06
1068377,92634,98.89
4691951,164807,98.93
1091006,55346,98.53
891230,30612,88.39
720734,35647,98.11
1025214,19926,94.86
505146,3309,59.69
265430,2124,91.99
3651318,283911,98.64
1290212,201881,99.3
420393,5434,95.99
655107,21485,96.16
1010207,23720,95.85
777547,9167,94.46
686703,34001,98.54
1625877,62101,98.35
2107926,59334,97.3
1564214,81581,97.96
2277765,53425,89.82
1558609,95695,98.23
1689305,88050,95.43
3382856,74078,93.32
4835746,276098,94.3
248754,2041,90.75
687182,63309,97.61
751948,24359,98.3
737756,23093,82.35
964229,18898,86.34
973121,22810,97.6
575508,16975,94.75
1114419,35208,94.3
722956,21843,97.6
1560200,38185,96.52
281397,3706,91.53
1122525,28232,97.23
20650480,212862,91.88
225207,1524,84.76
598367,24260,94.51
2117363,162960,99.12
1233027,16400,88.81
2566897,112005,54.67
11907188,1234111,83.49
1477059,36018,98.75
292469,5656,92.71
466862,47754,98.96
1055798,46122,97.84
1278142,26021,97.37
1938747,16942,87.66
338563,8416,96.46
645274,17943,94.67
730110,26868,92.31
1521090,19761,86.6
1719425,79646,98.33
3028604,75484,97.22
1236239,55409,96.0
906642,14128,91.88
1257902,20899,92.93
1163635,30173,89.82
1413936,90918,97.87
709519,6013,95.14
628111,41450,97.03
2478832,143686,98.28
2524598,32486,93.66
821547,18708,97.31
3016943,38294,95.76
743575,20181,89.7
919626,22114,95.84
2536083,538376,99.6
959442,13220,95.94
2044159,41080,92.48
1554417,67165,93.0
2181022,180132,98.19
1010899,13696,97.57
2620663,72681,96.68
5732609,189529,97.16
1187273,73120,99.24
1594532,85661,97.01
8403016,294629,96.97
5972754,133474,96.6
6189511,267690,99.03
1042734,23761,91.61
9476773,417402,97.8
8040754,789213,98.73
2724624,88968,91.74
1085592,27288,98.51
3393417,219213,95.68
16396012,208578,79.21
3226905,19814,91.77
6276301,286642,98.15
647094,19753,89.98
8081040,477122,98.81
886934,29360,98.46
1228396,29893,98.2
697471,6452,94.85
1605670,78364,96.63
2056991,121925,98.44
397981,6185,58.36
2760289,106828,97.14
3655043,54069,89.65
10662064,320959,97.89
3105500,108620,96.6
2238691,48825,96.77
1153518,25832,96.44
686228,24882,96.57
7523411,614901,98.87
2641916,49354,95.78
11657853,233343,97.82
5932061,172195,95.91
6313988,323119,98.18
2850316,218273,98.14
2620142,36637,93.99
854120,54821,98.05
13799864,317613,96.07
906841,35315,98.09
689607,20658,98.58
441729,14901,99.0
797800,14327,95.41
1682016,75706,98.17
1426251,57965,98.73
2268534,91796,97.75
750032,39406,98.19
4272799,26229,98.03
2449662,80825,97.54
5988592,512483,99.4
3662227,75552,97.46
725964,42700,98.98
1647440,111190,98.85
985104,12721,96.5
1665692,23961,92.37
2051794,81790,96.64
4112883,116481,93.46
33297045,1293427,99.07
1517628,19931,96.25
1675692,18803,72.76
3626738,173591,98.44
1169663,7766,92.99
446959,4923,89.48
6995153,195994,96.69
519706,18975,98.94
4373224,169228,93.01
4024087,73080,97.71
731349,42205,98.52
94366013,4539630,97.66
2458132,34337,95.52
1812670,17476,94.43
2028445,158178,97.94
1335703,12622,94.14
938717,17120,97.26
2926955,42554,97.73
4018930,32919,82.1
6439402,81148,51.58
5665790,166892,96.95
899728,28115,96.49
2792057,206926,96.99
12839663,722491,97.84
5694139,146797,98.19
1069693,3970,90.66
590760,70454,99.18
319347,1208,92.5
27594927,1351963,96.4
26993425,437561,97.42

data.csv

Comments 💬