在matplotlib中按组以长格式绘制数据

时间:2019-09-09 20:35:22

标签: python pandas matplotlib

我有以下数据:

import pandas as pd
import matplotlib.pyplot as plt
test = pd.DataFrame({"series":["plot1","plot1","plot1","plot2","plot2","plot2"],
                     "ind_var": [0,1,2,0,1,2],
                     "dep_var": [0,1,2,0,2,4]})

plot1f(x)=x描述,而plot2g(x)=2x

我想在同一图中绘制两个系列,并让它们共享相同的x和y轴。我碰到了this solution,但是它要求数据必须是 wide 格式(每组需要一行代码,我觉得很不方便)。

我想知道是否有与color中的ggplot2参数类似的东西。也就是说,将绘制由test["series"]分组的一列的参数。当有多个序列时,这样做的优势更加明显,因此您不必为每个序列写一行。

我发现了一个similar question,它与我能得到的接近:

# test.set_index("ind_var", inplace=True) This is the line I was missing
test.groupby("series")["dep_var"].plot(legend=True)
plt.show()

Almost

是否有一种方法可以使用grooupby沿线在单个图形中绘制两个系列?

0 个答案:

没有答案