简体   繁体   English

为使用 matplotlib 绘制熊猫数据框设置不同的标记大小

[英]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:我为每一行设置了不同的样式(我新知道有 6 个),现在我想对大小做同样的事情,这不起作用:

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.在这里,我每次将其减少 4,但是您想要减少每个标记大小的起始大小和数量显然取决于您。

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. markersize只接受一个浮点值,而不是根据文档。 You can use matplotlib instead, and plot each line independently您可以改用 matplotlib,并独立绘制每条线

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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