python通过matplotlib绘制常见的几种图形
一、使用matplotlib对几种常见的图形进行绘制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline #写了这个就可以不用写plt.show()
plt.rcparams[ 'font.sans-serif' ] = [ 'simhei' ] #用来正常显示中文标签
plt.rcparams[ 'axes.unicode_minus' ] = false #用来正常显示负号
x = np.linspace( 0 , 2 * np.pi, 100 ) # 均匀的划分数据
y = np.sin(x)
y1 = np.cos(x)
plt.title( "hello world!!" )
plt.plot(x,y)
plt.plot(x,y1)
|
1
2
3
4
5
6
7
8
|
x = np.linspace( 0 , 2 * np.pi, 100 )
y = np.sin(x)
y1 = np.cos(x)
plt.subplot( 211 ) # 等价于 subplot(2,1,1) #一个图版画两个图
plt.plot(x,y)
plt.subplot( 212 )
plt.plot(x,y1,color = 'r' )
|
1、柱状图
1
2
|
data = [ 5 , 25 , 50 , 20 ]
plt.bar( range ( len (data)),data)
|
2、水平绘制柱状图
1
2
|
data = [ 5 , 25 , 50 , 20 ]
plt.barh( range ( len (data)),data)
|
3、多个柱状图
1
2
3
4
5
6
7
8
9
10
11
|
data = [[ 5 , 25 , 50 , 20 ],
[ 4 , 23 , 51 , 17 ],
[ 6 , 22 , 52 , 19 ]]
x = np.arange( 4 )
plt.bar(x + 0.00 , data[ 0 ], color = 'b' , width = 0.25 ,label = "a" )
plt.bar(x + 0.25 , data[ 1 ], color = 'g' , width = 0.25 ,label = "b" )
plt.bar(x + 0.50 , data[ 2 ], color = 'r' , width = 0.25 ,label = "c" )
# 显示上面设置的 lable
plt.legend()
|
4、叠加型柱状图
1
2
3
4
5
6
7
8
9
10
|
data = [[ 5 , 25 , 50 , 20 ],
[ 4 , 23 , 51 , 17 ],
[ 6 , 22 , 52 , 19 ]]
x = np.arange( 4 )
plt.bar(x, data[ 0 ], color = 'b' , width = 0.25 )
plt.bar(x, data[ 1 ], color = 'g' , width = 0.25 ,bottom = data[ 0 ])
plt.bar(x, data[ 2 ], color = 'r' , width = 0.25 ,bottom = np.array(data[ 0 ]) + np.array(data[ 1 ]))
plt.show()
|
5、散点图
1
2
3
4
5
|
n = 50
x = np.random.rand(n)
y = np.random.rand(n)
plt.scatter(x, y)
|
6、气泡图
1
2
3
4
5
6
7
|
n = 50
x = np.random.rand(n)
y = np.random.rand(n)
colors = np.random.randn(n) # 颜色可以用数值表示
area = np.pi * ( 15 * np.random.rand(n)) * * 2 # 调整大小
plt.scatter(x, y, c = colors, alpha = 0.5 , s = area)
|
1
2
3
4
5
|
n = 50
x = np.random.rand(n)
y = np.random.rand(n)
colors = np.random.randint( 0 , 2 ,size = 50 )
plt.scatter(x, y, c = colors, alpha = 0.5 ,s = area)
|
7、直方图
1
2
3
|
a = np.random.rand( 100 )
plt.hist(a,bins = 20 )
plt.ylim( 0 , 15 )
|
1
2
3
|
a = np.random.randn( 10000 )
plt.hist(a,bins = 50 )
plt.title( "标准正太分布" )
|
8、箱线图
1
2
3
4
5
6
7
|
x = np.random.randint( 20 , 100 ,size = ( 30 , 3 ))
plt.boxplot(x)
plt.ylim( 0 , 120 )
# 在x轴的什么位置填一个 label,我们这里制定在 1,2,3 位置,写上 a,b,c
plt.xticks([ 1 , 2 , 3 ],[ 'a' , 'b' , 'c' ])
plt.hlines(y = np.median(x,axis = 0 )[ 0 ] ,xmin = 0 ,xmax = 3 )
|
二、添加文字描述
1、文字描述一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 设置画布颜色为 blue
fig, ax = plt.subplots(facecolor = 'blue' )
# y 轴数据
data = [[ 5 , 25 , 50 , 20 ],
[ 4 , 23 , 51 , 17 ],
[ 6 , 22 , 52 , 19 ]]
x = np.arange( 4 )
plt.bar(x + 0.00 , data[ 0 ], color = 'darkorange' , width = 0.25 ,label = 'a' )
plt.bar(x + 0.25 , data[ 1 ], color = 'steelblue' , width = 0.25 ,label = "b" )
plt.bar(x + 0.50 , data[ 2 ], color = 'violet' , width = 0.25 ,label = 'c' )
ax.set_title( "figure 2" )
plt.legend()
# 添加文字描述 方法一
w = [ 0.00 , 0.25 , 0.50 ]
for i in range ( 3 ):
for a,b in zip (x + w[i],data[i]):
plt.text(a,b, "%.0f" % b,ha = "center" ,va = "bottom" )
plt.xlabel( "group" )
plt.ylabel( "num" )
plt.text( 0.0 , 48 , "text" )
|
2、文字描述二
1
2
3
4
5
6
7
8
9
10
11
12
13
|
x = np.linspace( 0 , 2 * np.pi, 100 ) # 均匀的划分数据
y = np.sin(x)
y1 = np.cos(x)
plt.plot(x,y)
plt.plot(x,y1)
plt.annotate( 'points' ,
xy = ( 1 , np.sin( 1 )),
xytext = ( 2 , 0.5 ), fontsize = 16 ,
arrowprops = dict (arrowstyle = "->" ))
plt.title( "这是一副测试图!" )
|
三、多个图形描绘 subplots
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
% pylab inline
pylab.rcparams[ 'figure.figsize' ] = ( 10 , 6 ) # 调整图片大小
# np.random.seed(19680801)
n_bins = 10
x = np.random.randn( 1000 , 3 )
fig, axes = plt.subplots(nrows = 2 , ncols = 2 )
ax0, ax1, ax2, ax3 = axes.flatten()
colors = [ 'red' , 'tan' , 'lime' ]
ax0.hist(x, n_bins, normed = 1 , histtype = 'bar' , color = colors, label = colors)
ax0.legend(prop = { 'size' : 10 })
ax0.set_title( 'bars with legend' )
ax1.hist(x, n_bins, normed = 1 , histtype = 'bar' , stacked = true)
ax1.set_title( 'stacked bar' )
ax2.hist(x, n_bins, histtype = 'step' , stacked = true, fill = false)
ax2.set_title( 'stack step (unfilled)' )
# make a multiple-histogram of data-sets with different length.
x_multi = [np.random.randn(n) for n in [ 10000 , 5000 , 2000 ]]
ax3.hist(x_multi, n_bins, histtype = 'bar' )
ax3.set_title( 'different sample sizes' )
|
四、使用pandas 绘图
1、散点图
1
2
3
4
|
import pandas as pd
df = pd.dataframe(np.random.rand( 50 , 2 ), columns = [ 'a' , 'b' ])
# 散点图
df.plot.scatter(x = 'a' , y = 'b' )
|
2、绘制柱状图
1
2
3
|
df = pd.dataframe(np.random.rand( 10 , 4 ),columns = [ 'a' , 'b' , 'c' , 'd' ])
# 绘制柱状图
df.plot.bar()
|
3、堆积的柱状图
1
2
|
# 堆积的柱状图
df.plot.bar(stacked = true)
|
4、水平的柱状图
1
2
|
# 水平的柱状图
df.plot.barh(stacked = true)
|
5、直方图
1
2
3
4
|
df = pd.dataframe({ 'a' :np.random.randn( 1000 ) + 1 , 'b' :np.random.randn( 1000 ), 'c' :np.random.randn( 1000 ) - 1 }, columns = [ 'a' , 'b' , 'c' ])
# 直方图
df.plot.hist(bins = 20 )
|
6、箱线图
1
2
3
|
# 箱线图
df = pd.dataframe(np.random.rand( 10 , 5 ), columns = [ 'a' , 'b' , 'c' , 'd' , 'e' ])
df.plot.box()
|
到此这篇关于python通过matplotlib绘制常见的几种图形(推荐)的文章就介绍到这了,更多相关python matplotlib内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.51cto.com/u_11949039/3603586