簡體   English   中英

如何在MatplotLib中使用子圖繪制BarPlot或直方圖?

[英]How to draw BarPlot or Histogram using Subplot in MatplotLib?

  • 我想為數據繪制條形圖/直方圖的網格。我的數據包含1個NUMERIC和3個CATEGORICAL Column
  • PAIRGraph不適合我的目的,因為我只有1個數字列和3個分類列

試圖參考文檔https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html

但是,我找不到滿足我要求的確切方法。

使用演示代碼,我只能繪制LineGraph。 但是,我需要繪制條形圖。

fig, axes = plt.subplots(1, 2, figsize=(10,4))
x = np.linspace(0, 5, 11)
axes[0].plot(x, x**2, x, np.exp(x),x,20*x)
axes[0].set_title("Normal scale")
axes[0].plot

axes[1].plot(x, x**2, x, np.exp(x))
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)");

在我剛開始學習時,請隨時糾正我的方法或指導我。 在此處輸入圖片說明

如果您確切指定要用於barhist ,則可以修改,但通常只是將plot更改為所需的圖表類型

import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1, 2, figsize=(10,4))
x = np.linspace(0, 5, 11)
axes[0].bar(x,x**2) # bar plot
axes[0].set_title("Normal scale")
axes[0].plot

axes[1].hist(x) # histogram
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)");

plt.show()

在通過Matplotlip子圖軸查看API文檔之后,我發現了繪制不同圖形(不僅是折線圖)的方法。

https://matplotlib.org/api/axes_api.html

默認:-

  • axes[0].plot默認繪制折線圖。

自定義圖片:-

  • axes[0].bar可用於在所選 中繪制BAR圖

  • axes[0].scatter可用於在所選 中繪制散點圖

  • axes[0].hist可用於繪制直方圖 在所選子圖中

像上面的示例一樣,可以使用以下API繪制更多圖形:- 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM