RoQuOTriX
RoQuOTriX

Reputation: 3001

Set different markersizes for plotting pandas dataframe with matplotlib

I want to decrease the markersize for every line I plot with my dataframe. I can set a unique markersize like that:

df = pd.read_csv(file_string, index_col=0)
df.plot(style=['^-','v-','^-','v-','^-','v-'], markersize=8)

I set a different style for every line (I new that there are 6), now I wanted to do the same with the sizes, this doesn't work:

df = pd.read_csv(file_string, index_col=0)
df.plot(style=['^-','v-','^-','v-','^-','v-'], markersize=[16,14,12,10,8,6])

How can I achieve something like this?

Upvotes: 0

Views: 223

Answers (2)

Joe
Joe

Reputation: 206

The above earlier answer works fine for a small number of columns. If you don't want to repeat the same code many times, you can also write a loop that alternates between the markers, and reduces the marker size at each iteration. Here I reduced it by 4 each time, but the starting size and amount you want to reduce each marker size is obviously up to you.

df = pd.DataFrame({'y1':np.random.normal(loc = 5, scale = 10, size = 20),
                   'y2':np.random.normal(loc = 5, scale = 10, size = 20),
                   'y3':np.random.normal(loc = 5, scale = 10, size = 20)})
size = 18
for y in df.columns:
    col_index = df.columns.get_loc(y)
    if col_index % 2 == 0:    
        plt.plot(df[y], marker = '^', markersize = size)
    else:
        plt.plot(df[y], marker = 'v', markersize = size)
    size -= 4
plt.legend(ncol = col_index+1, loc = 'lower right')

enter image description here

Upvotes: 1

Igna
Igna

Reputation: 1127

markersize accepts only a float value not a l ist acording to the documentation. You can use matplotlib instead, and plot each line independently

import matplotlib.pyplot as plt
import pandas as pd


df = pd.read_csv(file_string, index_col=0)
plt.plot(df[x], df[y1],markersize=16,'^-')
plt.plot(df[x], df[y2],markersize=14,'v-')

#and so on...

plt.show() 

Upvotes: 0

Related Questions