Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a series as markersize in python plt.plot

Is it possible to use a column in a dataframe to scale the marker size in matplotlib? I keep getting an error about using a series when I do the following.

import pandas as pd
import matplotlib.pyplot as plt

my_dict = {'Vx': [16,25,85,45], 'r': [1315,5135,8444,1542], 'ms': [10,50,100, 25]}
df= pd.DataFrame(my_dict)
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.plot(df.Vx, df.r, '.', markersize= df.ms)

when I run

ValueError: setting an array element with a sequence.

I'm guessing it does not like the fact that Im feeding a series to the marker, but there must be a way to make it work...

like image 818
cmj29607 Avatar asked Aug 27 '26 15:08

cmj29607


1 Answers

Use plt.scatter instead of plt.plot. Scatter lets you specify the size s as well as the color c of the points using a tuple or list.

import pandas as pd
import matplotlib.pyplot as plt

my_dict = {'Vx': [16,25,85,45], 'r': [1315,5135,8444,1542], 'ms': [10,50,100, 25]}
df= pd.DataFrame(my_dict)
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.scatter(df.Vx, df.r, s= df.ms)
plt.show()
like image 178
ImportanceOfBeingErnest Avatar answered Aug 29 '26 05:08

ImportanceOfBeingErnest