首页 文章

图形轴在Python中无法正确显示

提问于
浏览
0

我正在尝试在python中创建一个2x2图表并且正在与轴进行斗争 . 这是我到目前为止所得到的 - 每个子图上的轴都搞砸了 .

这是我的代码:

def plotCarBar(df):
    fig = plt.figure()
    j = 1
    for i in pandaDF.columns[15:18]:
        cat_count = df.groupby(i)[i].count().sort_values().plot(figsize= 12,12), kind = 'line')
        ax = fig.add_subplot(2, 2, j)
        j += 1
    return ax.plot(lw = 1.3)

plotCarBar(pandaDF)

有人可以帮忙吗?提前致谢!

1 回答

  • 0

    我不确定你是否需要两个循环 . 如果您发布一些示例数据,我们可以更好地了解您的 cat_count 行正在做什么 . 目前,我不确定你是否需要两个计数器( ij ) .

    一般来说,我也建议直接使用 matplotlib ,除非你真的只是在熊猫中做一些快速和肮脏的绘图 .

    所以,这样的事情可能有效:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    randoms = np.random.rand(10, 4) # generate some data
    print(randoms)
    
    fig = plt.figure()
    
    for i in range(1, randoms.shape[1] + 1): # number of cols
        ax = fig.add_subplot(2, 2, i)
        ax.plot(randoms[i, :])
    plt.show()
    

    输出:

    [[0.78436298 0.85009767 0.28524816 0.28137471]
     [0.58936976 0.00614068 0.25312449 0.58549765]
     [0.24216048 0.13100618 0.76956316 0.66210005]
     [0.95156085 0.86171181 0.40940887 0.47077143]
     [0.91523306 0.33833055 0.74360696 0.2322519 ]
     [0.68563804 0.69825892 0.5836696  0.97711073]
     [0.62709986 0.44308186 0.24582971 0.97697002]
     [0.04356271 0.01488111 0.73322443 0.04890864]
     [0.9090653  0.25895051 0.73163902 0.83620635]
     [0.51622846 0.6735348  0.20570992 0.13803589]]
    

相关问题