子图二用两个不同的函数matplotlib作图

2024-05-29 06:53:40 发布

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

我找不到任何地方如何用不同的函数绘制相邻的两个图。有人能帮我吗?我和Jupyter笔记本一起工作。你知道吗

第一个功能:

def OccurrenciesPlotting(filtered_sent):
    fdist = FreqDist(filtered_sent)      
    fdist.plot(40,cumulative=False)

辅助功能:

def MyWordCloud(filtered_sent):
    img  = np.array(Image.open("H....jpg")) 
    stopwords = set(STOPWORDS)
    .
    .
    .

    image_colors = ImageColorGenerator(img)

    plt.figure()
    plt.imshow(wordcloud.recolor(color_func=image_colors),
               interpolation="bilinear")
    plt.axis("off")
    plt.savefig('wordcloud.png', format="png")
    plt.show()

我想要函数的结果一个接一个

非常感谢:)


Tags: 函数image功能imgpngdef地方绘制
1条回答
网友
1楼 · 发布于 2024-05-29 06:53:40

对于绘制子地块,必须使用plt子地块()功能。你的图形是包含两个子图的图形,每个子图位于一个轴对象上。 由于你的代码是不可复制的,因为我们缺乏数据,我不能提供完整的代码你。 通常,应该传递希望函数绘制的轴作为参数传递。 作为pd导入 导入matplotlib.pyplot文件作为plt

df = pd.DataFrame({"a":[1,2,3], "b": [2,3,4]})

def f1(df, axis):
    return df["a"].plot(ax = axis)

def f2(df, axis):
    axis = plt.plot(df["b"], c = "g")
    return axis

fig, axes = plt.subplots(nrows =1, ncols = 2)
f1(df, axes[0])
axes[1] = f2(df, axes[1])
plt.show()

如您所见,在使用pandas plotting函数和matplotlibs函数时,必须使用稍微不同的方法。可以将所需的轴传递给测向图()使用ax参数。为了使用常规matplotlib库,我从函数中返回了一个axis对象。你知道吗

相关问题 更多 >

    热门问题