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

时间:2018-12-08 23:46:11

标签: python python-3.x pandas matplotlib

我正在尝试在python中创建2x2图形,并且在轴上苦苦挣扎。这就是我到目前为止所得到的-每个子图上的轴都弄乱了。

enter image description here

这是我的代码:

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 :(得分: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]]

matplotlib plot

相关问题