Python 熊猫,多行的绘图选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14178194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python pandas, Plotting options for multiple lines
提问by Joerg
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
In style I can't use 's' and 'o' or any other alphabetical symbol, when defining colors in a list
线宽不能用列表调用
在样式中,在列表中定义颜色时,我不能使用 's' 和 'o' 或任何其他字母符号
Also there is some more stuff which seems weird to me
还有一些我觉得很奇怪的东西
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 plotIf 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
当我向上面的代码添加另一个绘图命令时,
testdataframe[0].plot()它会在同一个图中绘制这条线,如果我添加命令testdataframe[[0,1]].plot(),它将创建一个新图如果我将其称为
testdataframe[0].plot(style=['s-','o-','^-'],color=['b','r','y'])样式列表很好,但不能使用颜色列表
Hope somebody can help, thanks.
希望有人能帮忙,谢谢。
采纳答案by Paul H
You're so close!
你离得那么近!
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)
Also note that the plotmethod can take a matplotlib.axesobject, so you can make multiple calls like this (if you want to):
另请注意,该plot方法可以接受一个matplotlib.axes对象,因此您可以像这样进行多次调用(如果您愿意):
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.
在这种情况下不太实用,但这个概念以后可能会派上用场。
回答by piRSquared
Considering the dataframe testdataframe
考虑数据框 testdataframe
testdataframe = pd.DataFrame(np.arange(12).reshape(4,3))
print(testdataframe)
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
You can combine stylesinto a single list of strings as in stylesdefined below. I'll also define the linewidths in lws
您可以组合styles成一个字符串列表,如下styles定义。我还将定义线宽lws
styles=['bs-', 'ro-', 'y^-']
lws = [2, 1, 1]
We can use the plotmethod on the testdataframepassing the list stylesto the styleparameter. Note that we could have also passed a dictionary (and probably other things as well).
我们可以使用plot的方法testdataframe列表传递styles到style参数。请注意,我们还可以传递字典(可能还有其他东西)。
However, line widths are not as easily handled. I first capture the AxesSubplotobject and iterate over the lines attribute setting the line width.
然而,线宽并不那么容易处理。我首先捕获AxesSubplot对象并迭代设置线宽的线属性。
ax = testdataframe.plot(style=styles)
for i, l in enumerate(ax.lines):
plt.setp(l, linewidth=lws[i])
回答by RexFuzzle
So I think the answer lies in passing the color and style in the same argument. The following example works with pandas 0.19.2:
所以我认为答案在于在同一个论点中传递颜色和样式。以下示例适用于 Pandas 0.19.2:
testdataframe=pd.DataFrame(np.arange(12).reshape(4,3))
testdataframe.plot(style=['r*-','bo-','y^-'], linewidth=2.0)
Unfortunately, it seems that passing multiple line widths as an input to matplotlib is not possible.
不幸的是,似乎不可能将多个线宽作为输入传递给 matplotlib。


