前面我们讲过,好的图表在论文写作中是相当重要的,这里学姐为继续为大家分享一些Matplotlib快速入门内容以及论文绘图的技巧,帮助大家快速学习绘图。这里整理了完整的文档与技巧,有需要的同学看文章最后,另外,如果没有美赛经验想要获奖,欢迎咨询哦~
plt.fill_between(x, y1, y2, where, color, alpha)
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
plt.axes([0.025,0.025,0.95,0.95])
plt.plot (X, Y+1, color='blue', alpha=1.00)
plt.fill_between(X, 1, Y+1, color='blue',
alpha=.25)
plt.plot (X, Y-1, color='blue', alpha=1.00)
plt.fill_between(X, -1, Y-1, (Y-1) > -1,
color='blue', alpha=.25)
plt.fill_between(X, -1, Y-1, (Y-1) < -1,
color='red', alpha=.25)
plt.xlim(-np.pi,np.pi), plt.xticks([])
plt.ylim(-2.5,2.5), plt.yticks([])
# savefig(' ./figures/plot_ex.png',dpi=48)
plt.show()
scatter()
前面已经详细讲过,可以看上一篇文章哦。
import numpy as np
import matplotlib.pyplot as plt
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
T = np.arctan2(Y,X) # T中包含了数据点的颜色到当前
colormap的映射值
# print(T.shape)
plt.axes([0.025,0.025,0.95,0.95])
plt.scatter(X,Y, s=75, c=T, alpha=.5)
plt.xlim(-1.5,1.5), plt.xticks([])
plt.ylim(-1.5,1.5), plt.yticks([])
# savefig(' ./figures/scatter_ex.png',dpi=48)
plt.show()
直方图和条形图外观上看上去差不多,但概念和实现上完全不同,需要加以区分:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei']
# 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False
# 正常显示负号
# 随机生成(10000,)服从正态分布的数据
data = np.random.randn(10000)
"""
绘制直方图
data:必选参数,绘图数据
bins:直方图的长条形数目,可选项,默认为10
normed:是否将得到的直方图向量归一化,可选项,默认为0,代表
不归一化,显示频数。normed=1,表示归一化,显示频率。
facecolor:长条形的颜色
edgecolor:长条形边框的颜色
alpha:透明度
"""
plt.hist(data, bins=40, normed=0,
facecolor="blue", edgecolor="black", alpha=0.7)
# 显示横轴标签
plt.xlabel("区间")
# 显示纵轴标签
plt.ylabel("频数/频率")
# 显示图标题
plt.title("频数/频率分布直方图")
plt.show()
bar()
from pylab import *
n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) *
np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) *
np.random.uniform(0.5,1.0,n)
bar(X, +Y1, facecolor='#9999ff',
edgecolor='white')
bar(X, -Y2, facecolor='#ff9999',
edgecolor='white')
#xticks(X)
for x,y in zip(X,Y1):
text(x, y+0.05, '%.2f' % y, ha='center', va=
'bottom')
for x, y in zip(X, -Y2)
text(x, y-0.15, '%.2f'% y, ha='center',
va='bottom')
ylim(-1.25,+1.25)
show()
12345678910111213141516171819
import matplotlib.pyplot as plt
import matplotlib
"""
多个数据样本进行对比时,要注意每个数据样本对应的颜色,对每
个条形的注释文本设置和横纵坐标的设置
"""
# 设置中文字体和负号正常显示
matplotlib.rcParams['font.sans-serif'] =
['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
label_list = ['2014', '2015', '2016', '2017'] #
横坐标刻度显示值
num_list1 = [20, 30, 15, 35] # 纵坐标值1
num_list2 = [15, 30, 40, 20] # 纵坐标值2
x = range(len(num_list1))
# 绘制条形图
rects1 = plt.bar(x, height=num_list1, width=0.4,
alpha=0.5, color='red', label='部门一')
rects2 = plt.bar([i+0.4 for i in x],
height=num_list2, width=0.4, color='green',
label='部门二')
# 设置y轴属性
plt.ylim(0, 50)
plt.ylabel('数量')
# 设置x轴属性
plt.xticks([index+0.2 for index in x],
label_list)
plt.xlabel("年份")
plt.title('某某公司')
plt.legend()
# 显示文本
for rect in rects1:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2,
height + 1, str(height), ha='center',
va='bottom')
for rect in rects2:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2,
height + 1, str(height), ha='center',
va='bottom')
plt.show()
bar(y, width, height, left, *, align=‘center’, *kwargs)
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] =
['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
price = [39.5, 39.9, 45.4, 38.9, 33.34]
# 绘制水平条形图
plt.barh(range(5), price, height=0.7,
color='steelblue', alpha=0.5)
plt.yticks(range(5), ['亚马逊', '当当网', '中国图书
网', '京东', '天猫'])
plt.xlim(30, 47)
plt.xlabel('价格')
plt.title('不同平台图书价格')
for x, y in enumerate(price):
plt.text(y+0.2, x-0.1, '%s'%y)
plt.show()
import matplotlib.pyplot as plt
import matplotlib
# 设置中文字体和负号正常显示
matplotlib.rcParams['font.sans-serif'] =
['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
label_list = ['2014', '2015', '2016', '2017']
num_list1 = [20, 30, 15, 35]
num_list2 = [15, 33, 40, 20]
y = range(1, len(num_list1)+1)
y = [index*1.5 for index in y]
plt.barh(y, num_list1, height=0.4,
color='steelblue', alpha=0.5)
plt.barh([index-0.4 for index in y], num_list2,
height=0.4, color='red', alpha=0.5)
plt.yticks([index-0.2 for index in y],
label_list)
plt.ylabel('年份')
plt.xlim(0, 45)
plt.xlabel('数量')
for x, y1 in zip(num_list1, y):
plt.text(x+0.8, y1-0.1, str(x), ha='center',
va='bottom')
for x, y2 in zip(num_list2, y):
plt.text(x+0.8, y2-0.5, str(x), ha='center',
va='bottom')
plt.show()
import matplotlib.pyplot as plt
x = [52, 69, 58, 12, 39, 75]
y = [56, 15, 84, 65, 45, 48]
index = np.arange(len(x))
width = 0.3
plt.bar(index, height=x, width=width,
color='blue', label=u'x', alpha=0.5)
plt.bar(index, height=y, width=width,
color='gold', label=u'y') # 第二个图不能设置alpha
值,不然透明的两个条形会出现重叠
plt.xlabel('index')
plt.ylabel('x/y')
plt.title('barplot stack', fontsize=20,
color='gray')
plt.legend(loc='best')
plt.show()
X, Y = np.meshgrid(X, Y)
假设X为m维向量,Y为n维向量:
这样做可以使得X和Y中的每两个值互相都可以组成一个坐标点(xi ,y j ) (x{i}, y{j}) (xi,yj),在将这些坐标点作为输入,通过一个映射函数 f ( x ) f(x) f(x)求值,就可以得到一个三维图形。
例如: X = [ 1 , 2 , 3 ] , Y = [ 4 , 5 , 6 , 7 ] X = [1,2,3], Y=[4, 5, 6, 7] X=[1,2,3],Y=[4,5,6,7], 则 X , Y = n p . m e s h g r i d ( X , Y ) X,Y=np.meshgrid(X, Y) X,Y=np.meshgrid(X,Y)得到的结果为:
X = [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ] X = [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] X=[[1,2,3],[1,2,3], [1,2,3],[1,2,3]]
Y = [ [ 4 , 4 , 4 ] , [ 5 , 5 , 5 ] , [ 6 , 6 , 6 ] , [ 7 , 7 , 7 ] ] Y = [[4, 4, 4], [5, 5, 5], [6, 6, 6], [7, 7, 7]] Y=[[4,4,4], [5,5,5],[6,6,6],[7,7,7]]
plt.contour()
这个函数用于绘制等高线图
import matplotlib.pyplot as plt
def f(x,y): return (1-x/2+x *5+y *3)*np.exp(-
x *2-y *2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)
#print(X,'----', Y)
plt.contourf(X, Y, f(X,Y), 8, alpha=.75,
cmap='jet')
C = plt.contour(X, Y, f(X,Y), 8, colors='black')
show()