I am trying to plot a line using canvas and matplotlib.
I already have the Figure, and everything else. But when i try to use the command to plot(self.axes.plot()) it does not work.
Does anyone knows what is happening?? Probably there is some detail that i am missing, but i can't find it!!
import sys
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Plotting")
self.setGeometry(50, 50, 700, 700)
self.UI()
self.show()
def UI(self):
# Cria o canvas para exibição dos entes da estrutura ------------------
self.dpi = 70
self.fig = Figure((5.0, 5.0), dpi=self.dpi, frameon=False, tight_layout=True)
self.canvas = FigureCanvas(self.fig)
self.axes = self.fig.add_subplot(111)
# Cria grid de plotagem (axes) ----------------------------------------
self.load_axes()
layout = QGridLayout()
layout.addWidget(self.canvas, 0, 0)
self.setLayout(layout)
self.plotar()
def plotar(self):
self.axes.clear()
self.load_axes()
self.axes.plot([0, 0], [1, 1], linestyle = '-', color = 'black', linewidth=5.5, zorder = 0)
# Define a área de plotagem -------------------------------------------
self.axes.relim()
# Plota todas as linhas no canvas -------------------------------------
self.fig.canvas.draw()
def load_axes(self):
self.axes.axis('equal')
self.axes.set_xmargin(.4)
self.axes.set_ymargin(.4)
self.axes.autoscale_view(tight=True)
self.axes.xaxis.set_major_locator(plt.MultipleLocator(5.0))
self.axes.xaxis.set_minor_locator(plt.MultipleLocator(1.0))
self.axes.yaxis.set_major_locator(plt.MultipleLocator(5.0))
self.axes.yaxis.set_minor_locator(plt.MultipleLocator(1.0))
self.axes.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='gainsboro')
self.axes.grid(which='minor', axis='x', linewidth=0.50, linestyle='-', color='gainsboro')
self.axes.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='gainsboro')
self.axes.grid(which='minor', axis='y', linewidth=0.50, linestyle='-', color='gainsboro')
def main():
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())
if __name__=='__main__':
main()
It looks like it's actually drawing fine, no problem, but there is an issue with the ranges that you've specified for the axis.
You've specified: self.axes.plot([0, 0], [1, 1], linestyle = '-', color = 'black', linewidth=5.5, zorder = 0)
which means you're asking the x axis to display a range of 0 to 0 and the y axis to display 1 to 1. Try changing it to this:
self.axes.plot([0, 1], [0, 1], linestyle = '-', color = 'black', linewidth=5.5, zorder = 0)
That should display your line: :-D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.