简体   繁体   English

如何在MatplotLib中使用子图绘制BarPlot或直方图?

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

  • I want to draw Grid of Bar graph/Histogram for my data.My Data contains 1 NUMERIC and 3 CATEGORICAL Column 我想为数据绘制条形图/直方图的网格。我的数据包含1个NUMERIC和3个CATEGORICAL Column
  • PAIRGraph is not suitable for my purpose as my purpose as I have only 1 Numeric and 3 Categorical Column PAIRGraph不适合我的目的,因为我只有1个数字列和3个分类列

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 如果您确切指定要用于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()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM