问题描述
我想从熊猫数据框中绘制多条线,并为每条线设置不同的选项.我想做类似的事情
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 commandtestdataframe[[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,多行绘图选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!