【问题标题】:how to show multiple Matplotlib in one figure using python?如何使用 python 在一个图中显示多个 Matplotlib?
【发布时间】:2020-10-24 13:43:51
【问题描述】:

我有一个 python 脚本,可以将 histogramwordcloud 绘制在 2 个单独的图中。

我想要的是将这两个结果绘制成一个数字。

代码:

#plot word count distribution for both positive and negative sentiment
x= def_test_twtr_preds["processed word count"][def_test_twtr_preds.predictions ==1]
y= def_test_twtr_preds["processed word count"][def_test_twtr_preds.predictions ==0]
fig1 = plt.figure(figsize=(12,6))
plt.xlim(0,45)
plt.xlabel("word count")
plt.ylabel("frequency")
g = plt.hist([x,y],color=["r","b"],alpha=0.5,label=["positive","negative"])
plt.legend(loc="upper left")

# splt sentence to get individual words
all_words=[]
for line in def_test_twtr_preds["tokens"]: 
    all_words.extend(line)

#create a word frequency dictionary
wordfreq = Counter(all_words)

# #draw a word cloud with word frequencies
wordcloud = WordCloud(width=900,
                     height = 500,
                     max_words=500,
                     max_font_size=100,
                     relative_scaling=0.5,
                     colormap="Blues",
                     normalize_plurals=True).generate_from_frequencies(wordfreq)


fig2 = plt.figure(figsize=(17,14))
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.show()

【问题讨论】:

    标签: python matplotlib histogram word-cloud


    【解决方案1】:

    为此,您可以使用 ma​​tplotlib 中的 subplot 方法。看看这个“图1”是如何生成的https://matplotlib.org/3.1.1/gallery/subplots_axes_and_figures/multiple_figs_demo.html

    对于更复杂的子图,请查看子图文档https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我无法完全重写您的代码,因为它似乎有很大一部分丢失了,但是像这样重写绘图部分应该可以解决问题:

      fig = plt.figure(figsize=(30,30)) # Change to whichever size you want
      ax1 = plt.subplot(121)
      ax1.set_xlim(0,45)
      ax1.set_xlabel("word count")
      ax1.set_ylabel("frequency")
      g = ax1.hist([x,y],color=["r","b"],alpha=0.5,label=["positive","negative"])
      plt.legend(loc="upper left")
      
      # splt sentence to get individual words
      all_words=[]
      for line in def_test_twtr_preds["tokens"]: 
          all_words.extend(line)
      
      #create a word frequency dictionary
      wordfreq = Counter(all_words)
      
      # #draw a word cloud with word frequencies
      wordcloud = WordCloud(width=900,
                           height = 500,
                           max_words=500,
                           max_font_size=100,
                           relative_scaling=0.5,
                           colormap="Blues",
                           normalize_plurals=True).generate_from_frequencies(wordfreq)
      
      
      ax2 = plt.subplot(122)
      ax2.imshow(wordcloud,interpolation="bilinear")
      plt.show()
      

      查看@leo-aimone 的

      【讨论】:

      • @Isterzinger 如果我想绘制超过 2 个数字,让我们说 4 它不是这样的:ax3=plt.subplot(123) ax3 = plt.barh(words_df['word'], width = words_df['count']) ax4=plt.subplot(124) ax4=sns.heatmap(cm, annot=labels, fmt="", cmap='Blues') 因为我收到了这个错误:ValueError: num must be 1 <= num <= 2, not 3
      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 2021-07-22
      • 2017-05-20
      • 2022-06-29
      相关资源
      最近更新 更多