[英]How to draw BarPlot or Histogram using Subplot in MatplotLib?
試圖參考文檔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)");
如果您確切指定要用於bar
和hist
,則可以修改,但通常只是將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
可用於繪制直方圖 。 在所選子圖中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.