Matplotlib:多行p

2024-04-19 17:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据框如下所示:

Bin           A      B      C   Proba-a%   Proba-b%   Proba-c%    gamma%
CPB%                                                                    
0.100     20841  23195  24546  34.503457  27.103303  22.859837     0.100
0.200      2541   4187   5176  26.517913  13.944499   6.593338     0.200
0.300      2750   1823   1122  17.875550   8.215217   3.067253     0.300
0.400       999    829    448  14.736015   5.609856   1.659334     0.400
0.500       604    495    217  12.837838   4.054181   0.977373     0.500
0.600       436    348    116  11.467630   2.960495   0.612822     0.600
0.700       367    247     76  10.314268   2.184230   0.373979     0.700
0.800       305    186     35   9.355751   1.599673   0.263985     0.800
0.900       280    115     24   8.475801   1.238254   0.188561     0.900
1.000       200    102     18   7.847266   0.917691   0.131992     1.000

我想做的是在x轴上有“gamma%”和一个有三条线a、B和C的图表。我在某处看到你必须调用多次plt,我试过了:

^{pr2}$

但我有以下错误:

ValueError: Using arbitrary long args with data is not supported due to ambiguity of arguments.
Use multiple plotting calls instead.

有什么想法吗?谢谢!在


Tags: 数据databinis错误with图表args
3条回答

实际上你的方法很好,只是不要显式地说x='gamma%'。相反,只要像'gamma%'那样传递列名,它就可以工作了

示例:

plt.plot('gamma%', 'A', data=df_b)

这里它直接来自documentation

Plotting labelled data

There's a convenient way for plotting objects with labelled data (i.e. data that can be accessed by index obj['y']). Instead of giving the data in x and y, you can provide the object in the data parameter and just give the labels for x and y:

plot('xlabel', 'ylabel', data=obj)

也可以直接使用DataFrame以循环方式打印三列,而不必编写以下三个单独的plot命令

fig, ax = plt.subplots()

for cols in ['A', 'B', 'C']:
    df_b.plot('gamma%', cols, ax=ax)

enter image description here

相关问题 更多 >