常见的Python框架--matplotlib

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40722661/article/details/85269724

matplotlib

获取方法:

https://matplotlib.org/users/installing.html#building-on-linux

sudo apt-get install python-matplotlib #python2.
sudo apt-get install python3-matplotlib #python3.

简介

用于数据可视化

#easy example
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 1, 50)
y = 2*x + 1
# y = x**2
plt.plot(x, y)
plt.show()

二维图

点线图

在这里插入图片描述

t = np.arrange(0.0, 5.0, .2)
l = plt.plot(t1,f(t1),'ro')
plt.setp(l, markersize=30)
plt.setp(l, markerfacecoloe='Co')

plt.show()

plt.plot()

#函数原型
matplotlib.pyplot.plot(* args,scalex = True,scaley = True,data = None** kwargs)

格式化字符串

color

‘b’ 蓝色 ‘g’ 绿色 ‘r’ 红色 ‘c’ 青色 ‘m’ 品红
‘y’ 黄色 ‘k’ 黑色 ‘w’ 白色
或者matplotlib.colors

plt.plot(x, y, '.', color='#000000')
#点 黑色
plt.plot(x, y, '.', color='green')
#点 绿色
plt.plot(x, y, 'b.')
#点 蓝色

marker
常见:
‘.’ 点 | ‘o’ 圆圈 | ‘^’ 三角形 | ‘s’ 正方形 | ‘-’ 线 | ‘x’ 叉❌
‘*’ 星| ‘+’ ➕号

linestyle
‘-’ solid line style
‘–’ dashed line style
‘-.’ dash-dot line style
‘:’ dotted line style

经过测试发现,将这些参数放在格式化字符串之后,没办法改变之前的样式,建议:除了color,一般写在格式化字符串中。

plt.plot(x, y, marker='*') #不建议操作,会出现覆盖的现象
plt.plot(x, y, '*') #建议操作

条形图

plt.bar

N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
             bottom=menMeans, yerr=womenStd)

可选参数
yerr/xerr,误差范围
ecolor, 错误栏的线条颜色
color,颜色
bottom,位于该柱形下的数据

**注意:**对参数进行修改可以做到多数据的叠加

rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
                color='SkyBlue', label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
                color='IndianRed', label='Women')

官网链接:https://matplotlib.org/
官网案例:https://matplotlib.org/gallery/index.html#text-labels-and-annotations
基础教程:https://morvanzhou.github.io/tutorials/data-manipulation/plt/2-1-basic-usage/

猜你喜欢

转载自blog.csdn.net/weixin_40722661/article/details/85269724