您的位置:首页 > 编程语言 > PHP开发

matplotlib数据展现的基本用法

2017-12-19 11:30 696 查看

figure图像

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-3, 3, 50)

y1 = 2*x + 1

y2 = x**2

plt.figure()

plt.plot(x, y1)




plt.figure(num=3, figsize=(8, 5),)

plt.plot(x, y2)

# plot the second curve in this figure with certain parameters

plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')

plt.show()


使用plt.figure定义一个图像窗口:编号为3;大小为(8, 5). 使用plt.plot画(x ,y2)曲线. 使用plt.plot画(x ,y1)曲线,曲线的颜色属性(color)为红色;曲线的宽度(linewidth)为1.0;曲线的类型(linestyle)为虚线. 使用plt.show显示图像.



坐标轴

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-3, 3, 50)

y1 = 2*x + 1

y2 = x**2

plt.figure()

plt.plot(x, y2)

# plot the second curve in this figure with certain parameters

plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')

#使用plt.xlim设置x坐标轴范围:(-1, 2); 使用plt.ylim设置y坐标轴范围:(-2, 3); 使用plt.xlabel设置x坐标轴名称:’I am #x’; 使用plt.ylabel设置y坐标轴名称:’I am y’;
# set x limits

plt.xlim((-1, 2))

plt.ylim((-2, 3))

plt.xlabel('I am x')

plt.ylabel('I am y')

# set new sticks
#使用np.linspace定义范围以及个数:范围是(-1,2);个数是5. 使用print打印出新定义的范围. 使用plt.xticks设置x轴刻度:范围是#(-1,2);个数是5.

new_ticks = np.linspace(-1, 2, 5)

print(new_ticks)

plt.xticks(new_ticks)

# set tick labels
#使用plt.yticks设置y轴刻度以及名称:刻度为[-2, -1.8, -1, 1.22, 3];对应刻度的名称为[‘really bad’,’bad’,’normal’,’good’, ‘really good’]. 使用plt.show显示图像.

plt.yticks([-2, -1.8, -1, 1.22, 3],

[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])

plt.show()


使用plt.gca获取当前坐标轴信息. 使用.spines设置边框:右侧边框;使用.set_color设置边框颜色:默认白色; 使用.spines设置边框:上边框;使用.set_color设置边框颜色:默认白色;

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-3, 3, 50)

y1 = 2*x + 1

y2 = x**2

plt.figure()

plt.plot(x, y2)

# plot the second curve in this figure with certain parameters

plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')

# set x limits

plt.xlim((-1, 2))

plt.ylim((-2, 3))

# set new ticks

new_ticks = np.linspace(-1, 2, 5)

plt.xticks(new_ticks)

# set tick labels

plt.yticks([-2, -1.8, -1, 1.22, 3],

['$really\ bad$', '$bad$', '$normal$', '$good$', '$really\ good$'])

# to use '$ $' for math text and nice looking, e.g. '$\pi$'

# gca = 'get current axis'

ax = plt.gca()

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')




- 调整坐标轴

使用.xaxis.set_ticks_position设置x坐标刻度数字或名称的位置:bottom.(所有位置:top,bottom,both,default,none)

使用.spines设置边框:x轴;使用.set_position设置边框位置:y=0的位置;(位置所有属性:outward,axes,data)

ax.xaxis.set_ticks_position('bottom')

# ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]

ax.spines['bottom'].set_position(('data', 0))

# the 1st is in 'outward' | 'axes' | 'data'

# axes: percentage of y axis

# data: depend on y data




使用.yaxis.set_ticks_position设置y坐标刻度数字或名称的位置:left.(所有位置:left,right,both,default,none)

使用.spines设置边框:y轴;使用.set_position设置边框位置:x=0的位置;(位置所有属性:outward,axes,data) 使用plt.show显示图像.

ax.yaxis.set_ticks_position('left')

# ACCEPTS: [ 'left' | 'right' | 'both' | 'default' | 'none' ]

ax.spines['left'].set_position(('data',0))

plt.show()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  plot python