[英]How to draw BarPlot or Histogram using Subplot in MatplotLib?
Tried to Refer Documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html 试图参考文档https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html
However, I am unable to find exact way to fulfill my requirement. 但是,我找不到满足我要求的确切方法。
Using Demo code I am able to draw only LineGraph. 使用演示代码,我只能绘制LineGraph。 However, I am required to draw Bar Graph. 但是,我需要绘制条形图。
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)");
Please feel free to correct my approach or guide me as I have just started learning. 在我刚开始学习时,请随时纠正我的方法或指导我。
If you specify exactly what you want to use for the bar
and hist
, I can modify, but generally it is simply changing the plot
to the type of chart you need 如果您确切指定要用于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()
After going through the API documentation from Matplotlip Subplot Axes, I found ways to draw different graph not just Line graph. 在通过Matplotlip子图轴查看API文档之后,我发现了绘制不同图形(不仅是折线图)的方法。
https://matplotlib.org/api/axes_api.html https://matplotlib.org/api/axes_api.html
DEFAULT:- 默认:-
axes[0].plot
by-default draws line graph. axes[0].plot
默认绘制折线图。 CUSTOM GRAPH:- 自定义图片:-
axes[0].bar
can be used to draw BAR graph in selected Subplot axes[0].bar
可用于在所选子 图中绘制BAR图
axes[0].scatter
can be used to draw Scatter graph in selected Subplot axes[0].scatter
可用于在所选子 图中绘制散点图
axes[0].hist
can be used to draw a histogram . axes[0].hist
可用于绘制直方图 。 in selected Subplot 在所选子图中 Like above example more graph can be drawn with below API:- 像上面的示例一样,可以使用以下API绘制更多图形:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.