简体   繁体   中英

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?

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')

在此处输入图片说明

markersize accepts only a float value not al 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() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM