matplotlib

1. 在pyqt界面创建matplotlib 图

import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend-qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt

# 创建图
figure = plt.figure(figsize=(8, 8))
# 创建坐标轴
ax = figure.subplots(1,1)
# 也可以一步生成
figure, ax = plt.subplots(figsize=(8, 8), ncols=1)
# 将 图绑定到画布上
canvas = FigureCanvas(figure)
# 在坐标轴画图
ax.scatter(, , c=, marker=<散点形状>,s=<散点大小>)
# 更新画布
canvas.draw()

# 将画图加入到某个layout, 在主页面中使用该layout
self.canvas_layout = QVBoxLayout()
self.widget = QWidget()
self.canvas_layout.addWidget(canvas)
self.widget.setLayout(self.canvas_layout)
self.scroller.setWidget(self.widget)

2. 对matplotlib 图进行优化

  1. 去除坐标轴
import matplotlib.pyplot as plt
plt.axis('off')
  1. 缩短画布与图形之间的距离
import matplotlib.pyplot as plt
plt.subplots_adjust(bottom=0.001, top=0.999, left=0.001, right=0.999)  # 控制图形到画布的距离
ps: 此函数除此之外还能设置图形间的距离
  1. 设置坐标轴刻度范围及间隔
import matplotlib.pyplot as plt
plt.xlim(, )
plt.ylim(, )
# 坐标轴间隔
import matplotlib.pyplot as plt
x_locator = plt.MultipleLocator()
y_locator = plt.MultipleLocator()
ax.xaxis.set_major_locator(x_locator)  # 将刻度间隔应用于某个图
ax.yaxis.set_major_locator(y_locator)  # 将刻度间隔应用于某个图
  1. 设置画布背景颜色
import matplotlib.pyplot as plt
figure = plt.figure(figsize=(8, 8))  # 设置画布大小
figure.patch.set_facecolor('black')  # 设置画布背景色
  1. 清空画布或坐标轴
ax.cla()

你可能感兴趣的:(matplotlib)