pyqt5 matplotlib 收集 记录 LTS

Matplotlib: Visualization with Python

官网
https://matplotlib.org/#

Matplotlib 中文网
https://www.matplotlib.org.cn/

pyqt5 官方例子:
https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py
用户说明
https://matplotlib.org/users/index.html

官方教程
https://matplotlib.org/tutorials/index.html

参考教程
https://matplotlib.org/resources/index.html

numpy 中文网
https://www.numpy.org.cn/

Pandas中文网
https://www.pypandas.cn/

matplotlib API:
https://matplotlib.org/api/index.html#toolkits
https://matplotlib.org/api/pyplot_summary.html

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xlim.html#matplotlib.pyplot.xlim
将 matplotlib 嵌入 PyQt5
https://zhuanlan.zhihu.com/p/26379590
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

《PYTHON数据可视化编程实战》:matplot的内容(不包括嵌入到pyqt5)相当一部分的问题都是参考这里的内容。
2.《Python科学计算(第二版)》:numpy和分形的相关知识主要参考了这里。相较于第一版语言上更加简洁,内容上更加丰富,清晰详细的例程,csdn上都有下载。另外数学不太好的同学(比如我)也可以多研究一下里面的例程。

matplotlib for python developers

可能是pyqt4的
看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个。
https://blog.youkuaiyun.com/weixin_30480583/article/details/99464690

Create

Develop publication quality plots with just a few lines of code
Use interactive figures that can zoom, pan, update…

测试 学习 环境

Anaconda JupyterLab
在这里插入图片描述

如何 原点 从0开始
import matplotlib.pyplot as plt
import numpy as np

bottom, top = plt.ylim()
plt.ylim(0, top)
left, right = plt.xlim()
plt.xlim(0, right)
print('bottom, top', bottom, top)
print('left, right', left, right)
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

python plt画图横纵坐标0点重合
https://blog.youkuaiyun.com/weixin_30657541/article/details/99901342

matplot画图坐标原点不重合的问题
https://blog.youkuaiyun.com/weixin_40240670/article/details/80655537

如何显示网格线

plt.grid()
在这里插入图片描述
PYthon——plt.scatter各参数详解
https://blog.youkuaiyun.com/qiu931110/article/details/68130199/
有散点图 各种示例

如何显示多个坐标图

plt.subplots(2, 2)# 4个坐标图
在这里插入图片描述

如何额外添加 辅助线 和点

在这里插入图片描述

plt.axvline(x=40, ls="-", c="green")#添加垂直直线
plt.scatter(300, 150, c="green", label='operating point')
坐标图组成部分 :

在这里插入图片描述

https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

动态绘制曲线
一个坐标图中 显示 多条曲线
例子1

https://blog.youkuaiyun.com/qq_39105012/article/details/88584124

import matplotlib
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout
import matplotlib.pyplot as plt
import numpy as np
import sys


class Main_window(QDialog):
    def __init__(self):
        super().__init__()

        # 几个QWidgets
        self.figure = plt.figure(facecolor='#FFD7C4') #可选参数,facecolor为背景颜色
        self.canvas = FigureCanvas(self.figure)
        self.button_draw = QPushButton("绘图")

        # 连接事件
        self.button_draw.clicked.connect(self.Draw)

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.button_draw)
        self.setLayout(layout)

    def Draw(self):
        AgeList = ['10', '21', '12', '14', '25']
        NameList = ['Tom', 'Jon', 'Alice', 'Mike', 'Mary']

        #将AgeList中的数据转化为int类型
        AgeList = list(map(int, AgeList))

        # 将x,y轴转化为矩阵式
        self.x = np.arange(len(NameList)) + 1
        self.y = np.array(AgeList)

        #tick_label后边跟x轴上的值,(可选选项:color后面跟柱型的颜色,width后边跟柱体的宽度)
        plt.bar(range(len(NameList)), AgeList, tick_label=NameList, color='green', width=0.5)

        # 在柱体上显示数据
        for a, b in zip(self.x, self.y):
            plt.text(a-1, b, '%d' % b, ha='center', va='bottom')

        #设置标题
        plt.title("Demo")
		
		#画图
        self.canvas.draw()
        # 保存画出来的图片
        plt.savefig('1.jpg')


# 运行程序
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = Main_window()
    main_window.show()
    app.exec()

在这里插入图片描述

python matplotlib绘制gif动图以及保存

https://blog.youkuaiyun.com/qq_28888837/article/details/85778395

理解matplotlib绘图

https://www.cnblogs.com/yxzfscg/p/4972257.html

Matplotlib pyplot嵌入PYQT5的实战与反思

https://blog.youkuaiyun.com/qq_31809257/article/details/89292824

将 matplotlib 嵌入 PyQt5

嵌入到QDialog中

#coding:utf-8

# 导入matplotlib模块并使用Qt5Agg
import matplotlib
matplotlib.use('Qt5Agg')
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets,QtGui
import matplotlib.pyplot as plt
import sys

class My_Main_window(QtWidgets.QDialog):
    def __init__(self,parent=None):
        # 父类初始化方法
        super(My_Main_window,self).__init__(parent)
        
        # 几个QWidgets
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.button_plot = QtWidgets.QPushButton("绘制")

        # 连接事件
        self.button_plot.clicked.connect(self.plot_)
        
        # 设置布局
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.button_plot)
        self.setLayout(layout)

    # 连接的绘制的方法
    def plot_(self):
        ax = self.figure.add_axes([0.1,0.1,0.8,0.8])
        ax.plot([1,2,3,4,5])
        self.canvas.draw()

# 运行程序
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = My_Main_window()
    main_window.show()
    app.exec()

嵌入到QMainWindow中

#coding:utf-8

# 导入必要的模块
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets,QtGui
import matplotlib.pyplot as plt
import sys

class My_Main_window(QtWidgets.QMainWindow):
    def __init__(self,parent=None):
        super(My_Main_window,self).__init__(parent)
        # 重新调整大小
        self.resize(800, 659)
        # 添加菜单中的按钮
        self.menu = QtWidgets.QMenu("绘图")
        self.menu_action = QtWidgets.QAction("绘制",self.menu)
        self.menu.addAction(self.menu_action)
        self.menuBar().addMenu(self.menu)
        # 添加事件
        self.menu_action.triggered.connect(self.plot_)
        self.setCentralWidget(QtWidgets.QWidget())

    # 绘图方法
    def plot_(self):
        # 清屏
        plt.cla()
        # 获取绘图并绘制
        fig = plt.figure()
        ax =fig.add_axes([0.1,0.1,0.8,0.8])
        ax.set_xlim([-1,6])
        ax.set_ylim([-1,6])
        ax.plot([0,1,2,3,4,5],'o--')
        cavans = FigureCanvas(fig)
        # 将绘制好的图像设置为中心 Widget
        self.setCentralWidget(cavans)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = My_Main_window()
    main_window.show()
    app.exec()

python使用matplotlib的savefig保存时图片保存不完整的问题
plt.colorbar()
plt.savefig(title, dpi=300, bbox_inches = 'tight')
plt.show()
中文显示
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签
plt.rcParams['axes.unicode_minus']=False
matplolib种横坐标斜着显示
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker


fig,ax  = plt.subplots(1,1)
plt.xticks(rotation=120)   # 设置横坐标显示的角度,角度是逆时针,自己看
tick_spacing = 3    # 设置密度,比如横坐标9个,设置这个为3,到时候横坐标上就显示 9/3=3个横坐标,

x_list = [1,2,3,4,5,6,7,8,9]
y_list = '1 1 1 2 2 2 3 3 3'.split()
ax.plot(x_list,y_list)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

Python数据可视化分析 matplotlib教程

https://www.cnblogs.com/linblogs/p/9642472.html
Python学习笔记–坐标轴范围

参靠视频:《Python数据可视化分析 matplotlib教程》链接:https://www.bilibili.com/video/av6989413/?p=6

所用的库及环境:

IDE:Pycharm

Python环境:python3.7

Matplotlib: Matplotlib 1.11

Numpy: Numpy1.15.

坐标轴范围

概念

根据需求调整坐标轴的范围
坐标轴范围调整
第一种形式
 通过plt.axis()可以查看图形的x轴的最小最大坐标和y轴的最小最大坐标 
 
  文档

axis文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axis.html#matplotlib.pyplot.axis
xlim文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xlim.html#matplotlib.pyplot.xlim
ylim文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.ylim.html#matplotlib.pyplot.ylim

三.结语:

感谢matplotlib,numply提供的文档,感谢麦子学院提供的视频教学

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值