多个轴和图

时间:2018-07-06 15:26:42

标签: pandas matplotlib

对不起,如果发布,那不是很好。这对我来说是第一个Stack Overflow。 我具有以下结构的数据集:

     Revolution1   Position1   Temperature1   Revolution2   Position2   Temperature2
        1/min        mm            C            1/min         m            C
        datas....

我根据时间绘制这些图。现在,我想为每个不同的单位设置一个新的y轴。所以我看了matplotlib示例,并写了这样的内容。 X表示X值,d表示熊猫数据框:

fig,host=plt.subplots()
fig.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()

uni_units = np.unique(units[1:])
par2.spines["right"].set_position(("axes", 1.2))

make_patch_spines_invisible(par2)
# Second, show the right spine.
par2.spines["right"].set_visible(True)
for i,v in enumerate(header[1:]):
    if d.loc[0,v] == uni_units[0]: 
        y=d.loc[an:en,v].values
        host.plot(x,y,label=v)
    if d.loc[0,v] == uni_units[1]:
      (v,ct_yax[1]))
        y=d.loc[an:en,v].values
        par1.plot(x,y,label=v)
    if d.loc[0,v] == uni_units[2]: 

        y=d.loc[an:en,v].values
        par2.plot(x,y,label=v)

see the plot i get here...

编辑:好吧,我真的很想问这个问题(也许我很紧张,因为这是第一次在这里发布):

我实际上想问为什么它不起作用,因为我只看到2个图。但是通过放大,我看到它实际上绘制了每条曲线...

对不起!

1 个答案:

答案 0 :(得分:1)

如果我正确理解,您想要从Dataframe获得子图。

您可以使用subplots对象下的plot函数中的Dataframe参数来实现此目的。

通过下面的玩具示例,您可以更好地了解如何实现这一目标:

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"y1":[1,5,3,2],"y2":[10,12,11,15]})
df.plot(subplots=True)
plt.show()

产生如下图: enter image description here

您可以选中documentation about subplots for pandas Dataframe

相关问题