Python可视化 调整颜色和样式,编程方式,子图方式,创建多个子图,创建网格,创建图例,坐标轴范围调整,调整坐标轴刻度,添加坐标轴

八种自建默认颜色缩写
b : blue
g : green
r : red
c : cyan
m : magenta
y: magenta
y : yellow
k :black
w:white
其他颜色表示方法
灰色阴影
html 十六进制
RGB元组

用数字表示,代表灰色程度有多深

y = np.arange(1, 5)
plt.plot(y+1, color='0.5') #灰色阴影
plt.plot(y+2, color='#FF00FF') #16进制
plt.plot(y+3, color=(0.1, 0.2, 0.3)) #RGB
plt.show()

在这里插入图片描述
查阅颜色链接:https://www.114la.com/other/rgb.htm

y = np.arange(1, 5)
plt.plot(y, marker='p') 
plt.plot(y+1, marker='o') 
plt.plot(y+2, marker='D') 
plt.plot(y+3, marker='^') 
plt.show()

在这里插入图片描述
注:如果写出marker会在图上加上线段, 没有加marker 图上就没有线段

y = np.arange(1, 5)
plt.plot(y, '--')
plt.plot(y+1, '-')
plt.plot(y+2, '^')
plt.plot(y+3, ':')
plt.show()

在这里插入图片描述
样式字符串
可以将颜色,点型,线型写成一个字符串
cx–
mo:
kp-

y = np.arange(1, 5)
plt.plot(y, 'cx--')
plt.plot(y+1, 'kp:')
plt.plot(y+2, 'mo-')
plt.plot(y+3, ':')
plt.show()

在这里插入图片描述
pyplot:经典高层封装,用的最多
pylab:将matplotlib 和Numpy合并的模块,模拟Matlab的编程环境
面向对象的方式:Matplotlib底层方式

pylab方式

from pylab import *
x = arange(0, 10, 1)
y = randn(len(x))
plot(x, y)
title('pylab')
show()

pyplot方式

from pylab import *
x = np.arange(0, 10, 1)
y = np.random.randn(len(x))
plt.plot(x, y)
plt.title('pyplot')
plt.show()

面向对象

x = np.arange(0, 10, 1)
y = np.random.randn(len(x))
fig = plt.figure()
ax = fig.add_subplot(111)
l, = plt.plot(x, y)
t = ax.set_title('object oriented')
plt.show()

用面向对象的方式 画出正弦函数

x = np.arange(-2*np.pi, 2*np.pi, 0.01)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
l, = plt.plot(x, y)
t = ax.set_title('object oriented')
plt.show()

在这里插入图片描述
子图方式
fig=plt.figure()
Figure实例
可以添加Axes实例
ax=fig.add_subplot(111)
返回Axes实例
参数一:子图总行数
参数二:子图总列数
参数三,子图的位置

x = np.arange(1, 100)
fig = plt.figure()

ax1 = fig.add_subplot(231)
ax1.plot(x, x)
ax2 = fig.add_subplot(232)
ax2.plot(x, -x)
plt.show()

在这里插入图片描述
pyplot画出图形

x = np.arange(1, 100)
plt.subplot(221)
plt.plot(x, -x)


plt.subplot(222)
plt.plot(x, np.log(x))
plt.show()

在这里插入图片描述
画出多个子图

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot([1, 2, 3], [3, 2, 1])

ax2 = fig1.add_subplot(111)
ax2.plot([1, 2, 3], [1, 2, 3])
plt.show()

在这里插入图片描述
画出网格

y = np.arange(5)
plt.plot(y, y*2)
plt.grid(True)  #打开网格
plt.show()

在这里插入图片描述
更改网格参数

y = np.arange(5)
plt.plot(y, y*2)
plt.grid(linewidth='1', color='red', linestyle='--')  #打开网格
plt.show()

在这里插入图片描述
面向对象的方式绘制网格

x = np.arange(0, 10, 1)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(x, x*2)
ax.grid(color='g')
plt.show()

在这里插入图片描述
创建图例
创建每条线的信息框
plt的方式

 x = np.arange(1, 11, 1)
plt.plot(x, x*2, label='Normal')
plt.plot(x, x*3, label='Normal')
plt.plot(x, x*4, label='Normal')
plt.legend()
plt.show()

在这里插入图片描述
用loc参数对legend进行操作
更改图例位置

plt.legend(loc=1)

在这里插入图片描述

plt.legend(loc=3)

在这里插入图片描述
让图例扁平化一点
ncol弄成几列

plt.legend(ncol=3)

在这里插入图片描述
或者通过方式加上名字

x = np.arange(1, 11, 1)
plt.plot(x, x*2)
plt.plot(x, x*3)
plt.plot(x, x*4)
plt.legend(['1', '2', '3'])
plt.show()

在这里插入图片描述
坐标轴范围调整

x = np.arange(-10, 11, 1)
plt.plot(x, x*x)
plt.axis([-5, 5, 20, 60]) #设置x / y轴范围
plt.show()

在这里插入图片描述

plt.xlim([-5,5]) #只更改x轴
plt.ylim([-5,5]) #只更改y轴

在这里插入图片描述
调整坐标轴

x = np.arange(1, 11, 1)
plt.plot(x, x)
ax = plt.gca() #获取当前图形的坐标轴
ax.locator_params('x', nbins=5) #只调整x轴
ax.locator_params('y', nbins=5) #只调整x轴
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib as mpl

fig = plt.figure()
start = datetime.datetime(2015, 1, 1)
stop = datetime.datetime(2016, 1, 1)
delta = datetime.timedelta(days=1)
dates = mpl.dates.drange(start, stop, delta)
y = np.random.rand(len(dates))
ax = plt.gca()
ax.plot_date(dates, y, linestyle='-', marker='')
date_format = mpl.dates.DateFormatter('%Y-%m')
ax.xaxis.set_major_formatter(date_format)
fig.autofmt_xdate()  #根据排列大小,坐标轴自适应
plt.show()

在这里插入图片描述
添加新的坐标轴
使用相同的横坐标,纵坐标刻度不同

x = np.arange(2, 20, 1)
y1 = x*x
y2 = np.log(x)
plt.plot(x, y1)
plt.twinx() #添加新的坐标轴
plt.plot(x, y2, 'red')
plt.show()

面向对象的方式

x = np.arange(2, 20, 1)
y1 = x*x
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('Y1')

ax2 = ax1.twinx()  #添加一个新的图形
ax2.plot(x, y2, 'red')
ax2.set_ylabel('Y2')
ax1.set_xlabel('Compare Y1 and Y2')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值