python 画组合图
Python是一个非常流行的编程语言,能够支持多种绘图操作,其中包括组合图。组合图是一种将区别类型的图形组合在一起以实现多个区别图像共存的绘图方式。在这篇文章中,我们将探讨怎样使用Python画组合图。
# 导入所需的库 import matplotlib.pyplot as plt import numpy as np # 设置随机种子 np.random.seed(19680801) # 准备数据 mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # 创建figure对象,设置其大小 fig = plt.figure(figsize=(8,6)) # 创建第一个子图,绘制直方图 ax1 = fig.add_subplot(2,1,1) n, bins, patches = ax1.hist(x, 50, density=1, alpha=0.75) ax1.set_xlabel('IQ') ax1.set_ylabel('Distribution') ax1.set_title('Histogram of IQ: $\mu=100$, $\sigma=15$') # 创建第二个子图,绘制散点图 ax2 = fig.add_subplot(2,1,2) y = x + 10 * np.random.randn(10000) ax2.scatter(x, y, alpha=0.3) ax2.set_xlabel('IQ') ax2.set_ylabel('Performance') ax2.set_title('Scatter plot of IQ vs Performance') # 展现图形 plt.show()
以上代码使用了matplotlib库,首先定义了一个随机变量x,然后创建了一个figure对象并设置了其大小。接着使用add_subplot()方法,分别创建了两个子图,在第一个子图中绘制了一个直方图,在第二个子图中绘制了一个散点图。最后使用show()方法将图形展现出来。
这是一个简单的组合图的例子,可以找到并使用更多的matplotlib库的功能来创建更复杂的组合图。
文章来源:丸子建站
文章标题:python 画组合图
https://www.wanzijz.com/view/59748.html