- Python:Data Analytics and Visualization
- Phuong Vo.T.H Martin Czygan Ashish Kumar Kirthi Raman
- 488字
- 2021-07-09 18:51:40
Exploring plot types
We have looked at how to create simple line plots so far. The matplotlib library supports many more plot types that are useful for data visualization. However, our goal is to provide the basic knowledge that will help you to understand and use the library for visualizing data in the most common situations. Therefore, we will only focus on four kinds of plot types: scatter plots, bar plots, contour plots, and histograms.
Scatter plots
A scatter plot is used to visualize the relationship between variables measured in the same dataset. It is easy to plot a simple scatter plot, using the plt.scatter()
function, that requires numeric columns for both the x
and y
axis:

Let's take a look at the command for the preceding output:
>>> X = np.random.normal(0, 1, 1000) >>> Y = np.random.normal(0, 1, 1000) >>> plt.scatter(X, Y, c = ['b', 'g', 'k', 'r', 'c']) >>> plt.show()
Bar plots
A bar plot is used to present grouped data with rectangular bars, which can be either vertical or horizontal, with the lengths of the bars corresponding to their values. We use the plt.bar()
command to visualize a vertical bar, and the plt.barh()
command for the other:

The command for the preceding output is as follows:
>>> X = np.arange(5) >>> Y = 3.14 + 2.71 * np.random.rand(5) >>> plt.subplots(2) >>> # the first subplot >>> plt.subplot(211) >>> plt.bar(X, Y, align='center', alpha=0.4, color='y') >>> plt.xlabel('x') >>> plt.ylabel('y') >>> plt.title('bar plot in vertical') >>> # the second subplot >>> plt.subplot(212) >>> plt.barh(X, Y, align='center', alpha=0.4, color='c') >>> plt.xlabel('x') >>> plt.ylabel('y') >>> plt.title('bar plot in horizontal') >>> plt.show()
Contour plots
We use contour plots to present the relationship between three numeric variables in two dimensions. Two variables are drawn along the x
and y
axes, and the third variable, z
, is used for contour levels that are plotted as curves in different colors:
>>> x = np.linspace(-1, 1, 255) >>> y = np.linspace(-2, 2, 300) >>> z = np.sin(y[:, np.newaxis]) * np.cos(x) >>> plt.contour(x, y, z, 255, linewidth=2) >>> plt.show()
Let's take a look at the contour plot in the following image:

Tip
If we want to draw contour lines and filled contours, we can use the plt.contourf()
method instead of plt.contour()
. In contrast to MATLAB, matplotlib's contourf()
will not draw the polygon edges.
Histogram plots
A histogram represents the distribution of numerical data graphically. Usually, the range of values is partitioned into bins of equal size, with the height of each bin corresponding to the frequency of values within that bin:

The command for the preceding output is as follows:
>>> mu, sigma = 100, 25 >>> fig, (ax0, ax1) = plt.subplots(ncols=2) >>> x = mu + sigma * np.random.randn(1000) >>> ax0.hist(x,20, normed=1, histtype='stepfilled', facecolor='g', alpha=0.75) >>> ax0.set_title('Stepfilled histogram') >>> ax1.hist(x, bins=[100,150, 165, 170, 195] normed=1, histtype='bar', rwidth=0.8) >>> ax1.set_title('uniquel bins histogram') >>> # automatically adjust subplot parameters to give specified padding >>> plt.tight_layout() >>> plt.show()
- 火格局的時空變異及其在電網防火中的應用
- 走入IBM小型機世界
- Dreamweaver CS3網頁設計50例
- 極簡AI入門:一本書讀懂人工智能思維與應用
- 統計學習理論與方法:R語言版
- 計算機網絡原理與技術
- 工業機器人安裝與調試
- Machine Learning with the Elastic Stack
- 在實戰中成長:Windows Forms開發之路
- Excel 2007終極技巧金典
- 筆記本電腦維修之電路分析基礎
- Learn Microsoft Azure
- Learning Cassandra for Administrators
- 機器學習案例分析(基于Python語言)
- 開放自動化系統應用與實戰:基于標準建模語言IEC 61499