Matplotlib自学打卡day5

Matplotlib自学打卡day5

pcolormesh

import matplotlib.pyplot as plt

import numpy as np

np.random.seed(19860801)

Z = np.random.rand(3, 5)  # C is a parameter in the pcolormesh, it should at least smaller than the len of x, y.
# there is a problem i can't solve.
# x = np.arange(-0.5, 10, 1)  # len = 11
# y = np.arange(4.5, 11, 1)  # len = 7
x = np.arange(-0.5, 10, 2)  # len = 6
y = np.arange(4.5, 11, 2)  # len = 4
plt.pcolormesh(x, y, Z)
fig, ax = plt.subplots()
x1, y1 = np.meshgrid(x, y)
x1 = x1 + (0.2 * y1)
y1 = y1 + (.3 * x1)
plt.pcolormesh(x1, y1, Z)
plt.show()

Center Coordinate

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.ticker import MaxNLocator
from matplotlib.colors import BoundaryNorm
import numpy as np
np.random.seed(19860801)

Z = np.random.rand(6, 10)
x = np.arange(-0.5, 10, 1)
y = np.arange(4.5, 11, 1)
plt.pcolormesh(x, y, Z)
x1, y1 = np.meshgrid(x, y)
x1 = x1 + (y1*0.2)
y1 = y1 + (x1 * 0.2)
plt.pcolormesh(x1, y1, Z)

Z = np.random.rand(6, 10)
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
x = np.arange(10)
y = np.arange(6)
x2, y2 = np.meshgrid(x, y)
ax[1].pcolormesh(x2, y2, Z, vmin=np.min(Z), vmax=np.max(Z), shading='auto')
ax[1].set_title('auto')
ax[0].pcolormesh(x2, y2, Z[:-1, :-1], vmin=np.min(Z), vmax=np.max(Z), shading='flat')
ax[0].set_title('flat')


plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Matplotlib是一个用于绘制数据可视化图形的Python,而PyQt5是一个用于创建GUI应用程序的Python库。要在PyQt5中嵌入Matplotlib,可以使用Matplotlib的FigureCanvasQTAgg类。 以下是将Matplotlib嵌入PyQt5的基本步骤: 1. 导入所需的库: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure ``` 2. 创建一个继承自QMainWindow的主窗口类: ```python class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Matplotlib in PyQt5") self.setGeometry(100, 100, 800, 600) ``` 3. 创建一个继承自FigureCanvas的Matplotlib画布类: ```python class MatplotlibCanvas(FigureCanvas): def __init__(self, parent=None): self.fig = Figure() super().__init__(self.fig) ``` 4. 在主窗口类中添加Matplotlib画布: ```python class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Matplotlib in PyQt5") self.setGeometry(100, 100, 800, 600) self.canvas = MatplotlibCanvas(self) layout = QVBoxLayout() layout.addWidget(self.canvas) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) ``` 5. 在主窗口类中添加绘图函数,并在Matplotlib画布中调用该函数进行绘图: ```python class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Matplotlib in PyQt5") self.setGeometry(100, 100, 800, 600) self.canvas = MatplotlibCanvas(self) layout = QVBoxLayout() layout.addWidget(self.canvas) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.plot_data() def plot_data(self): ax = self.canvas.fig.add_subplot(111) ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) self.canvas.draw() ``` 6. 创建应用程序并运行: ```python if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 这样,就可以在PyQt5应用程序中嵌入Matplotlib绘图了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值