Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib and subplots properties

I'm adding a matplotlib figure to a canvas so that I may integrate it with pyqt in my application. I were looking around and using plt.add_subplot(111) seem to be the way to go(?) But I cannot add any properties to the subplot as I may with an "ordinary" plot

figure setup

self.figure1 = plt.figure() self.canvas1 = FigureCanvas(self.figure1) self.graphtoolbar1 = NavigationToolbar(self.canvas1, frameGraph1)  hboxlayout = qt.QVBoxLayout()  hboxlayout.addWidget(self.graphtoolbar1) hboxlayout.addWidget(self.canvas1)  frameGraph1.setLayout(hboxlayout) 

creating subplot and adding data

df = self.quandl.getData(startDate, endDate, company)  ax = self.figure1.add_subplot(111) ax.hold(False) ax.plot(df['Close'], 'b-') ax.legend(loc=0) ax.grid(True) 

I'd like to set x and y labels, but if I do ax.xlabel("Test")

AttributeError: 'AxesSubplot' object has no attribute 'ylabel' 

which is possible if I did it by not using subplot

plt.figure(figsize=(7, 4)) plt.plot(df['Close'], 'k-') plt.grid(True) plt.legend(loc=0) plt.xlabel('value') plt.ylabel('frequency') plt.title('Histogram') locs, labels = plt.xticks() plt.setp(labels, rotation=25) plt.show() 

So I guess my question is, is it not possible to modify subplots further? Or is it possible for me to plot graphs in a pyqt canvas, without using subplots so that I may get benefit of more properties for my plots.

like image 679
vandelay Avatar asked Jul 29 '16 15:07

vandelay


People also ask

What will PLT subplot 333 do?

subplot(333) do? Create a blank plot that fills the figure. Create a plot with three points at location (3,3). Create a smaller subplot in the topleft of the figure.

How do I use subplots in matplotlib?

Use plt. subplots to create figure and multiple axes (most useful) Rather than creating a single axes, this function creates a full grid of equal-sized axes in a single line, returning them in a NumPy array. You need to specify the no of rows and columns as an argument to the subplots() function.

What does the function subplot return in matplotlib?

Matplotlib - Subplots() Function The function returns a figure object and a tuple containing axes objects equal to nrows*ncols. Each axes object is accessible by its index.


1 Answers

plt.subplot returns a subplot object which is a type of axes object. It has two methods for adding axis labels: set_xlabel and set_ylabel:

ax = plt.subplot('111') ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') 

You could also call plt.xlabel and plt.ylabel (like you did before) and specify the axes to which you want the label applied.

ax = plt.subplot('111') plt.xlabel('X Axis', axes=ax) plt.ylabel('Y Axis', axes=ax) 

Since you only have one axes, you could also omit the axes kwarg since the label will automatically be applied to the current axes if one isn't specified.

ax = plt.subplot('111') plt.xlabel('X Axis') plt.ylabel('Y Axis') 
like image 103
Suever Avatar answered Sep 30 '22 09:09

Suever