35

I'm adding a matplotlib figure to a canvas so that I may integrate it with pyqt in my application. I was 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

figure1 = plt.figure()
canvas1 = FigureCanvas(figure1)
graphtoolbar1 = NavigationToolbar(canvas1, frameGraph1)

hboxlayout = qt.QVBoxLayout()
        
hboxlayout.addWidget(graphtoolbar1)
hboxlayout.addWidget(canvas1)
        
frameGraph1.setLayout(hboxlayout)

creating subplot and adding data

df = quandl.getData(startDate, endDate, company)
          
ax = 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 benefit from more properties for my plots?

cottontail
  • 10,268
  • 18
  • 50
  • 51
vandelay
  • 1,965
  • 8
  • 35
  • 52

2 Answers2

70

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')
Suever
  • 64,497
  • 14
  • 82
  • 101
  • That did the trick, thanks! Is it possible to rotate the x labels ? – vandelay Jul 29 '16 at 20:45
  • 1
    @vandelay Sure. You can use the `rotation` kwarg: `plt.xlabel('X Axis', rotation=30)` – Suever Jul 29 '16 at 20:48
  • I was more thinking with subplots :), as with ax. Thanks for answering. – vandelay Jul 29 '16 at 21:15
  • 1
    @vandelay you want to rotate the plot itself? You can also just do `label = ax.set_xlabel('X Axis')` and then `label.set_rotation(30)` – Suever Jul 29 '16 at 21:21
  • I'd like to rotate the data on the x axis. I got dates on the x axis and they're all horizontal and cross into eachother – vandelay Jul 29 '16 at 21:24
  • 1
    @vandelay Oh those are tick labels. see [here](http://stackoverflow.com/questions/29214290/rotated-axis-labels-are-placed-incorrectly-matplotlib) – Suever Jul 29 '16 at 21:25
  • 1
    That worked for me. Still, i find it very confusing to change the method's name when moving from plotting straight using pyplot (plt) to creating a figure and working with it. – Guy s Jul 24 '18 at 07:48
  • Can you make set_size on subplots? – Noob Programmer Sep 16 '19 at 10:15
0

Almost all matplotlib properties define a set() method. For example, Axes objects define a set() method that can be used to set axis labels, ticks, title etc. on subplots. Axis labels are Text objects (which can be accessed through ax.yaxis.label or ax.xaxis.label), which also define a set() method to change stuff.

fig, ax = plt.subplots(facecolor='white', figsize=(6,3))
ax.plot(range(5))
# set properties on this ax
ax.set(xlabel='X Axis', ylabel='Y Axis', title='Title', xticks=range(5))
ax.yaxis.label.set(rotation=0, ha='right')  # modify properties on ylabel
ax.title.set(fontsize=20);                  # change title properties

The very same job can also be done using set_* methods.

fig, ax = plt.subplots(facecolor='white', figsize=(6,3))
ax.plot(range(5))
ax.set_xlabel('X Axis')                          # set xlabel
ax.set_ylabel('Y Axis', rotation=0, ha='right')  # set ylabel with properties
ax.set_title('Title', fontsize=20)               # set title
ax.set_xticks(range(5));                         # set xticks

result

cottontail
  • 10,268
  • 18
  • 50
  • 51