0

I'm trying to make a simple graph in a PyQt5 application and I am running into some styling problems which is most likely due to how I'm initializing the classes/axes.

Here's the relvant code:

# Canvas
self.canvas = QWidget(self)
self.canvas.resize(820, 500)
self.canvas.move(-10, 200)
self.figure = plt.figure()
layout = QVBoxLayout(self.canvas)
self.static_canvas = FigureCanvas(self.figure)
layout.addWidget(self.static_canvas)
self.static_axis = self.static_canvas.figure.subplots()
self.static_axis.set_title('a sine wave')
self.canvas.hide()

def generate_graph(self):
    self.static_axis.clear()
    self.static_axis.plot(self.data_dict[self.graph_subject])
    self.static_canvas.draw()
    self.canvas.show()

Any ideas of how I could switch it up so that I can have the option to add custom x-ticks and axis labels? The trial self.static_axis.set_title('a sine wave') doesn't do anything, no errors or effects.

What it'd like to do is have options like this for example:

import numpy as np
import matplotlib.pyplot as plt

x1 = np.linspace(0.0, 5.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)

plt.subplot()
plt.plot(x1, y1, '.-')
plt.title('Test plot')
plt.ylabel('Damped oscillation')
plt.xticks(np.arange(0, 6, step=2.5), ["start", "half", "end"])

plt.show()
Marcus Grass
  • 1,043
  • 2
  • 17
  • 38

1 Answers1

0

Found the answer in this SO-post, ticks and labels need to be called after plot(), new working method call:

def generate_graph(self):
    self.static_axis.clear()
    self.static_axis.plot(self.data_dict[self.graph_subject])
    self.static_axis.set_xlabel('my xdata')
    self.static_axis.set_ylabel('my xdata')
    self.static_axis.set_title('a sine wave')
    self.static_canvas.draw()
    self.canvas.show()
Marcus Grass
  • 1,043
  • 2
  • 17
  • 38