[Matplotlib]不同类型图形的绘制
2019-01-31 16:30
232 查看
目录
目录
1、简单图形
[code]import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 2.0, 0.01) s = 1 + np.sin(2*np.pi*t) plt.plot(t, s) plt.xlabel('time (s)') plt.ylabel('voltage (mV)') plt.title('About as simple as it gets, folks') plt.grid(True) plt.savefig("test.png") plt.show()
2、多坐标的子图示例
[code]""" Simple demo with multiple subplots. """ import numpy as np import matplotlib.pyplot as plt x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1, 1) plt.plot(x1, y1, 'o-') plt.title('A tale of 2 subplots') plt.ylabel('Damped oscillation') plt.subplot(2, 1, 2) plt.plot(x2, y2, '.-') plt.xlabel('time (s)') plt.ylabel('Undamped') plt.savefig('test.png') plt.show()
3、频率直方图
[code]""" ========================================================= Demo of the histogram (hist) function with a few features ========================================================= In addition to the basic histogram, this demo shows a few optional features: * Setting the number of data bins * The ``normed`` flag, which normalizes bin heights so that the integral of the histogram is 1. The resulting histogram is an approximation of the probability density function. * Setting the face color of the bars * Setting the opacity (alpha value). Selecting different bin counts and sizes can significantly affect the shape of a histogram. The Astropy docs have a great section on how to select these parameters: http://docs.astropy.org/en/stable/visualization/histogram.html """ import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt np.random.seed(0) # example data mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x = mu + sigma * np.random.randn(437) num_bins = 50 fig, ax = plt.subplots() # the histogram of the data n, bins, patches = ax.hist(x, num_bins, normed=1) # add a 'best fit' line y = mlab.normpdf(bins, mu, sigma) ax.plot(bins, y, '--') ax.set_xlabel('Smarts') ax.set_ylabel('Probability density') ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$') # Tweak spacing to prevent clipping of ylabel fig.tight_layout() plt.savefig('test.png') plt.show()
ATTENTION:The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead. alternative="'density'", removal="3.1")
4、条形图
[code]""" Bar chart demo with pairs of bars grouped for easy comparison. """ import numpy as np import matplotlib.pyplot as plt n_groups = 5 means_men = (20, 35, 30, 35, 27) std_men = (2, 3, 4, 1, 2) means_women = (25, 32, 34, 20, 25) std_women = (3, 5, 2, 3, 3) fig, ax = plt.subplots() index = np.arange(n_groups) bar_width = 0.35 opacity = 0.4 error_config = {'ecolor': '0.3'} rects1 = plt.bar(index, means_men, bar_width, alpha=opacity, color='b', yerr=std_men, error_kw=error_config, label='Men') rects2 = plt.bar(index + bar_width, means_women, bar_width, alpha=opacity, color='r', yerr=std_women, error_kw=error_config, label='Women') plt.xlabel('Group') plt.ylabel('Scores') plt.title('Scores by group and gender') plt.xticks(index + bar_width / 2, ('A', 'B', 'C', 'D', 'E')) plt.legend() plt.tight_layout() plt.savefig("test.png") plt.show()
5、饼图
[code]""" =============== Basic pie chart =============== Demo of a basic pie chart plus a few additional features. In addition to the basic pie chart, this demo shows a few optional features: * slice labels * auto-labeling the percentage * offsetting a slice with "explode" * drop-shadow * custom start angle Note about the custom start angle: The default ``startangle`` is 0, which would start the "Frogs" slice on the positive x-axis. This example sets ``startangle = 90`` such that everything is rotated counter-clockwise by 90 degrees, and the frog slice starts on the positive y-axis. """ import matplotlib.pyplot as plt # Pie chart, where the slices will be ordered and plotted counter-clockwise: labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') fig1, ax1 = plt.subplots() ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.savefig("test.png") plt.show()
相关文章推荐
- 使用python matplotlib绘制简单图形
- Python使用matplotlib实现绘制自定义图形功能示例
- Python3使用Matplotlib 绘制精美的数学函数图形
- 使用matplotlib绘制多个图形单独显示
- matplotlib实战绘制各种图形
- Python实现在tkinter中使用matplotlib绘制图形的方法示例
- Python使用matplotlib绘制多个图形单独显示的方法示例
- 使用matplotlib绘制三维图形
- Python使用matplotlib绘制三维图形示例
- python中matplotlib绘制图形
- matplotlib 可视化 —— 绘制常见图形
- Matplotlib -多组线用不同的线性、颜色、节点绘制
- Python+matplotlib绘制不同大小和颜色散点图实例
- python中matplotlib绘制图形
- 输入方程式绘制具体图形(python-matplotlib)
- 不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决
- 使用matplotlib绘制自定义图形
- Python基于matplotlib实现绘制三维图形功能示例
- python中matplotlib绘制图形
- matplotlib绘制基本图形