Reputation: 727
I am learning python data analysis by reading other people's codes step by step.
I have a DataFrame named df
like this:
df.to_dict()
{'color map': {0: 'green',
1: 'green',
2: 'green',
3: 'green',
4: 'green',
5: 'green',
6: 'green',
7: 'green',
8: 'green',
9: 'green',
10: 'green',
11: 'green',
12: 'green',
13: 'green',
14: 'green',
15: 'green',
16: 'green',
17: 'green',
18: 'green',
19: 'green',
20: 'green',
21: 'green',
22: 'green',
23: 'green',
24: 'green',
25: 'green',
26: 'green',
27: 'green',
28: 'green',
29: 'green',
30: 'green',
31: 'green',
32: 'green',
33: 'green',
34: 'green',
35: 'green',
36: 'green',
37: 'green',
38: 'green',
39: 'green',
40: 'green',
41: 'green',
42: 'green',
43: 'green',
44: 'green',
45: 'green',
46: 'green',
47: 'green',
48: 'green',
49: 'green',
50: 'green',
51: 'green',
52: 'green',
53: 'green',
54: 'green',
55: 'green',
56: 'green',
57: 'green',
58: 'green',
59: 'green',
60: 'green',
61: 'red',
62: 'red',
63: 'red',
64: 'red',
65: 'red',
66: 'red',
67: 'red',
68: 'red',
69: 'red',
70: 'red',
71: 'red',
72: 'red',
73: 'red',
74: 'red',
75: 'red',
76: 'red',
77: 'red',
78: 'red',
79: 'red',
80: 'red',
81: 'red',
82: 'red',
83: 'red',
84: 'red',
85: 'red',
86: 'red',
87: 'red',
88: 'red',
89: 'red',
90: 'red',
91: 'red',
92: 'red',
93: 'red',
94: 'red',
95: 'red',
96: 'red',
97: 'red',
98: 'red',
99: 'red',
100: 'red',
101: 'red',
102: 'red',
103: 'red',
104: 'red',
105: 'red',
106: 'red',
107: 'red',
108: 'red',
109: 'red',
110: 'red',
111: 'red',
112: 'red',
113: 'red',
114: 'red',
115: 'red',
116: 'red',
117: 'red',
118: 'red',
119: 'red',
120: 'red',
121: 'red',
122: 'red',
123: 'red',
124: 'red',
125: 'red',
126: 'red',
127: 'red',
128: 'red',
129: 'red',
130: 'red',
131: 'red',
132: 'red',
133: 'red',
134: 'red',
135: 'red'},
'name': {0: 'Ivern',
1: 'Anivia',
2: 'Xerath',
3: 'Ahri',
4: 'Sona',
5: 'Skarner',
6: 'Zilean',
7: 'KogMaw',
8: 'Janna',
9: 'Pantheon',
10: 'Twitch',
11: 'Annie',
12: 'Warwick',
13: 'Draven',
14: 'Amumu',
15: 'Xin Zhao',
16: 'Nunu',
17: 'Blitzcrank',
18: 'Yasuo',
19: 'Leona',
20: 'Irelia',
21: 'Ziggs',
22: 'Tryndamere',
23: 'Kayle',
24: 'Miss Fortune',
25: 'Master Yi',
26: 'Soraka',
27: 'Vi',
28: 'Teemo',
29: 'Brand',
30: 'Shaco',
31: 'Darius',
32: 'VelKoz',
33: 'Illaoi',
34: 'Aatrox',
35: 'Riven',
36: 'Jarvan IV',
37: 'Karthus',
38: 'Twisted Fate',
39: 'Jinx',
40: 'Sejuani',
41: 'Jax',
42: 'Caitlyn',
43: 'Fiddlesticks',
44: 'Swain',
45: 'Kindred',
46: 'Wukong',
47: 'Taric',
48: 'Kled',
49: 'Lulu',
50: 'Yorick',
51: 'Thresh',
52: 'Kassadin',
53: 'Morgana',
54: 'Fiora',
55: 'Renekton',
56: 'Fizz',
57: 'Lucian',
58: 'Hecarim',
59: 'Bard',
60: 'Malphite',
61: 'Vayne',
62: 'Nami',
63: 'Orianna',
64: 'Nocturne',
65: 'Shyvana',
66: 'Katarina',
67: 'Garen',
68: 'Jayce',
69: 'Ashe',
70: 'Sion',
71: 'Braum',
72: 'Heimerdinger',
73: 'Syndra',
74: 'Tristana',
75: 'Rammus',
76: 'Zac',
77: 'Lux',
78: 'Zyra',
79: 'Evelynn',
80: 'Varus',
81: 'Ekko',
82: 'KhaZix',
83: 'Alistar',
84: 'Elise',
85: 'Volibear',
86: 'Veigar',
87: 'Taliyah',
88: 'Sivir',
89: 'Gangplank',
90: 'Talon',
91: 'Galio',
92: 'Xayah',
93: 'Diana',
94: 'Akali',
95: 'Singed',
96: 'Rumble',
97: 'DrMundo',
98: 'Maokai',
99: 'Kalista',
100: 'Gragas',
101: 'Shen',
102: 'Aurelion Sol',
103: 'ChoGath',
104: 'Lee Sin',
105: 'Cassiopeia',
106: 'Nautilus',
107: 'Lissandra',
108: 'Udyr',
109: 'Graves',
110: 'Olaf',
111: 'Corki',
112: 'Quinn',
113: 'RekSai',
114: 'Malzahar',
115: 'Karma',
116: 'Viktor',
117: 'Nasus',
118: 'Vladimir',
119: 'Trundle',
120: 'Tahm Kench',
121: 'Gnar',
122: 'Kennen',
123: 'Urgot',
124: 'Mordekaiser',
125: 'Camille',
126: 'Nidalee',
127: 'LeBlanc',
128: 'Zed',
129: 'Jhin',
130: 'Poppy',
131: 'Rengar',
132: 'Rakan',
133: 'Ezreal',
134: 'Azir',
135: 'Ryze'},
'total matches': {0: 6671,
1: 6433,
2: 5108,
3: 30841,
4: 11847,
5: 1720,
6: 4797,
7: 6440,
8: 21182,
9: 8872,
10: 18273,
11: 11585,
12: 15418,
13: 16427,
14: 10853,
15: 12106,
16: 4252,
17: 18545,
18: 24173,
19: 15019,
20: 9350,
21: 6575,
22: 11658,
23: 5129,
24: 12758,
25: 20382,
26: 11766,
27: 9903,
28: 11482,
29: 12896,
30: 9437,
31: 15022,
32: 8899,
33: 5239,
34: 3350,
35: 17855,
36: 7322,
37: 2353,
38: 7807,
39: 21777,
40: 9558,
41: 16466,
42: 47566,
43: 4839,
44: 5873,
45: 2651,
46: 6084,
47: 2877,
48: 6360,
49: 20821,
50: 3531,
51: 39734,
52: 12828,
53: 18794,
54: 15468,
55: 15130,
56: 17665,
57: 46914,
58: 7793,
59: 10422,
60: 8393,
61: 28374,
62: 15006,
63: 23636,
64: 4888,
65: 4327,
66: 9929,
67: 7603,
68: 7961,
69: 18461,
70: 3829,
71: 12090,
72: 3172,
73: 14887,
74: 10308,
75: 4337,
76: 9085,
77: 21329,
78: 9576,
79: 6171,
80: 7826,
81: 15482,
82: 16006,
83: 7302,
84: 11746,
85: 4216,
86: 9657,
87: 4534,
88: 5595,
89: 13843,
90: 5802,
91: 10215,
92: 23525,
93: 5506,
94: 8428,
95: 2560,
96: 6244,
97: 3220,
98: 10890,
99: 5470,
100: 17360,
101: 8289,
102: 1708,
103: 5815,
104: 48457,
105: 5333,
106: 10392,
107: 3580,
108: 3869,
109: 19035,
110: 5020,
111: 1927,
112: 2840,
113: 1714,
114: 4680,
115: 20065,
116: 5512,
117: 8114,
118: 9611,
119: 3305,
120: 4076,
121: 4978,
122: 7369,
123: 780,
124: 1603,
125: 3690,
126: 4520,
127: 8690,
128: 17687,
129: 11240,
130: 5165,
131: 6590,
132: 18339,
133: 23480,
134: 2597,
135: 4607},
'win rate': {0: 55.868685354519563,
1: 53.971708378672467,
2: 53.621769772905246,
3: 53.545604876625276,
4: 53.135814974255091,
5: 53.081395348837212,
6: 52.991452991452995,
7: 52.717391304347828,
8: 52.714568973656881,
9: 52.569882777276824,
10: 52.448968423356867,
11: 52.447129909365565,
12: 52.34141912050849,
13: 52.31022097765873,
14: 52.308117571178478,
15: 52.288121592598714,
16: 52.187206020696145,
17: 51.997843084389331,
18: 51.847102138749847,
19: 51.821026699513951,
20: 51.732620320855617,
21: 51.665399239543731,
22: 51.655515525819183,
23: 51.511015792552151,
24: 51.504938078068662,
25: 51.47679324894515,
26: 51.393846676865543,
27: 51.267292739573868,
28: 51.175753353074384,
29: 51.155397022332508,
30: 51.139133199109885,
31: 51.098389029423508,
32: 51.084391504663444,
33: 51.078450085894254,
34: 51.044776119402989,
35: 51.038924670960519,
36: 51.010652827096422,
37: 50.998725031874201,
38: 50.877417702062253,
39: 50.870184139229458,
40: 50.81607030759573,
41: 50.765213166524958,
42: 50.758945465248281,
43: 50.692291795825582,
44: 50.689596458368804,
45: 50.660128253489248,
46: 50.641025641025635,
47: 50.538755648244702,
48: 50.518867924528301,
49: 50.51150280966332,
50: 50.467289719626166,
51: 50.432878642975787,
52: 50.342999688182097,
53: 50.313929977652435,
54: 50.297388156193435,
55: 50.257766027759423,
56: 50.195301443532415,
57: 50.108709553651366,
58: 50.096240215578078,
59: 50.076760698522364,
60: 50.065530799475752,
61: 49.904842461408336,
62: 49.886711981873916,
63: 49.87307497038416,
64: 49.836333878887068,
65: 49.826669748093366,
66: 49.803605599758285,
67: 49.78298040247271,
68: 49.780178369551564,
69: 49.77520177671849,
70: 49.751893444763645,
71: 49.75186104218362,
72: 49.716267339218156,
73: 49.586887888762007,
74: 49.582848273185874,
75: 49.550380447313813,
76: 49.532195927352781,
77: 49.510056730273341,
78: 49.498746867167917,
79: 49.473343056230753,
80: 49.373881932021469,
81: 49.302415708564787,
82: 49.294014744470822,
83: 49.274171459874005,
84: 49.267835859015833,
85: 49.217267552182165,
86: 49.166407787097441,
87: 49.117776797529778,
88: 49.097408400357459,
89: 49.07895687351008,
90: 49.052051016890729,
91: 49.045521292217323,
92: 48.875664187035071,
93: 48.873955684707596,
94: 48.837209302325576,
95: 48.828125,
96: 48.814862267777066,
97: 48.633540372670808,
98: 48.558310376492194,
99: 48.555758683729437,
100: 48.513824884792626,
101: 48.498009410061528,
102: 48.419203747072601,
103: 48.40928632846088,
104: 48.360402005902145,
105: 48.340521282580163,
106: 48.325635103926096,
107: 48.324022346368714,
108: 48.307056086844149,
109: 48.205936432886787,
110: 48.167330677290835,
111: 48.157758173326414,
112: 48.133802816901408,
113: 48.133022170361727,
114: 48.119658119658119,
115: 48.11363070022427,
116: 48.040638606676346,
117: 48.015775203352227,
118: 47.997086671522219,
119: 47.927382753403933,
120: 47.76741903827282,
121: 47.609481719566091,
122: 47.414845976387568,
123: 47.307692307692307,
124: 47.286338116032439,
125: 46.720867208672082,
126: 46.637168141592923,
127: 46.628308400460298,
128: 46.429581048227512,
129: 46.387900355871885,
130: 46.331074540174249,
131: 45.720789074355082,
132: 45.591362669720269,
133: 45.468483816013631,
134: 44.050827878321144,
135: 38.897330149772088}}
I want to draw a scatter plot where total matches
as x-axis, win rate
as y-axis, each point colored by the corresponding color in color map
.
Like this(don't consider the name description):
The code is:
df.plot(kind='scatter', x='total matches', y='win rate', color=df['color map'].tolist(), figsize=(15,10), title='win rate vs # matches by champions')
But this raise ValueError: could not convert string to float: 'green'
How can I fix it?
Upvotes: 0
Views: 118
Reputation: 727
This should due to my pandas version. When I update my pandas, from 0.19.2
to 0.21.1
. The errors disappeared.
Upvotes: 0
Reputation: 31
This code will get what you're looking for, without the need to convert your whole dataframe to a dictionary. However, I did have to convert each Pandas column to a list in order to add the name label to each point, but without the name labels you could have used the dataframe directly.
# All the necessary imports
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
# There are several style templates in Matplotlib, this one seems to fit your look
style.use('fivethirtyeight')
# Reduce font size to reduce clutter
plt.rcParams['font.size'] = 6.0
fig, ax = plt.subplots()
# Create list from each dataframe column
x = df['total matches'].tolist()
y = df['win rate'].tolist()
colors = df['color map'].tolist()
names = df['name'].tolist()
# Create scatter plot, c property takes list of colors
ax.scatter(x, y, c=colors)
# Iterate through names to add label to each point
for i, txt in enumerate(names):
ax.annotate(txt, (x[i],y[i]))
# Show the chart, matplotlib can save the chart in several formats as well
plt.show()
Upvotes: 0
Reputation: 40878
This parameter syntax seems to work.
Given df
:
name total_matches win_rate color_map
0 Ivern 6671 55.87 green
1 Anivia 6433 53.97 red
2 Xerath 5108 53.62 blue
3 Ahri 30841 53.55 yellow
4 Sona 11847 53.14 black
Use:
df.plot(kind='scatter', x='total_matches',
y='win_rate', c=df.color_map, s=80)
Upvotes: 1