Python pandas ,多行的绘图选项 [英] Python pandas, Plotting options for multiple lines

查看:24
本文介绍了Python pandas ,多行的绘图选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从熊猫数据框中绘制多条线并为每条线设置不同的选项.我想做类似的事情

I want to plot multiple lines from a pandas dataframe and setting different options for each line. I would like to do something like

testdataframe=pd.DataFrame(np.arange(12).reshape(4,3))
testdataframe.plot(style=['s-','o-','^-'],color=['b','r','y'],linewidth=[2,1,1])

这将引发一些错误消息:

This will raise some error messages:

  • 线宽不能用列表调用

  • linewidth is not callable with a list

在样式中,当在列表中定义颜色时,我不能使用 's' 和 'o' 或任何其他字母符号

In style I can't use 's' and 'o' or any other alphabetical symbol, when defining colors in a list

还有一些我觉得很奇怪的东西

Also there is some more stuff which seems weird to me

  • 当我将另一个绘图命令添加到上述代码 testdataframe[0].plot() 时,如果我添加命令 testdataframe,它将在同一图中绘制这条线[[0,1]].plot() 它将创建一个新图

  • when I add another plot command to the above code testdataframe[0].plot() it will plot this line in the same plot, if I add the command testdataframe[[0,1]].plot() it will create a new plot

如果我调用 testdataframe[0].plot(style=['s-','o-','^-'],color=['b','r','y']) 样式列表可以,但颜色列表不行

If i would call testdataframe[0].plot(style=['s-','o-','^-'],color=['b','r','y']) it is fine with a list in style, but not with a list in color

希望有人能帮忙,谢谢.

Hope somebody can help, thanks.

推荐答案

你离我很近!

您可以在样式列表中指定颜色:

You can specify the colors in the styles list:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

testdataframe = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C'])
styles = ['bs-','ro-','y^-']
linewidths = [2, 1, 4]
fig, ax = plt.subplots()
for col, style, lw in zip(testdataframe.columns, styles, linewidths):
    testdataframe[col].plot(style=style, lw=lw, ax=ax)

另请注意,plot 方法可以接受一个 matplotlib.axes 对象,因此您可以像这样进行多次调用(如果您愿意):

Also note that the plot method can take a matplotlib.axes object, so you can make multiple calls like this (if you want to):

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

testdataframe1 = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C'])
testdataframe2 = pd.DataFrame(np.random.normal(size=(4,3)), columns=['D', 'E', 'F'])
styles1 = ['bs-','ro-','y^-']
styles2 = ['rs-','go-','b^-']
fig, ax = plt.subplots()
testdataframe1.plot(style=styles1, ax=ax)
testdataframe2.plot(style=styles2, ax=ax)

在这种情况下不太实用,但这个概念以后可能会派上用场.

Not really practical in this case, but the concept might come in handy later.

这篇关于Python pandas ,多行的绘图选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆