在Python中未正确显示图形轴

2024-06-25 05:26:59 发布

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

我正在尝试用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)

有人能帮忙吗?提前谢谢!你知道吗


Tags: in图形密码dfforplotdefcount
1条回答
网友
1楼 · 发布于 2024-06-25 05:26:59

我不确定你是否需要两个环。如果您发布一些示例数据,我们也许能够更好地理解您的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

相关问题 更多 >