I have an animation plot, with 4 sub plots. The plots ax1 and ax2 share the same scale - hence I could plot the ax2 y-axis on the right hand side y-axes of ax1.
ax1.plot(df.pnl.as_matrix(),color='black')
ax2.plot(df.unit.as_matrix(),color='black')
axi = ax1.twinx()
axi.set_ylim(ax2.get_ylim())
Which crates the following plot:
But obviously I don't want the ax2 subplot to contain the data from df.unit.as_matrix() - as I would rather use it for something else. So my question is, how to I get the axes that ax2 creates, without actually using ax2. I.e. is there a way to derive it from the matrix df.unit.as_matrix(), and somehow use that, instead of ax2.get_ylim().
In essense what I am asking, is what method does matplotlib use to create the axes from a 2D matrix - then I could just replicate that and use ax2 subplot for something more useful.
Related
In a previous answer it was recommended to me to use add_subplot instead of add_axes to show axes correctly, but searching the documentation I couldn't understand when and why I should use either one of these functions.
Can anyone explain the differences?
Common grounds
Both, add_axes and add_subplot add an axes to a figure. They both return a (subclass of a) matplotlib.axes.Axes object.
However, the mechanism which is used to add the axes differs substantially.
add_axes
The calling signature of add_axes is add_axes(rect), where rect is a list [x0, y0, width, height] denoting the lower left point of the new axes in figure coodinates (x0,y0) and its width and height. So the axes is positionned in absolute coordinates on the canvas. E.g.
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
places a figure in the canvas that is exactly as large as the canvas itself.
add_subplot
The calling signature of add_subplot does not directly provide the option to place the axes at a predefined position. It rather allows to specify where the axes should be situated according to a subplot grid. The usual and easiest way to specify this position is the 3 integer notation,
fig = plt.figure()
ax = fig.add_subplot(231)
In this example a new axes is created at the first position (1) on a grid of 2 rows and 3 columns. To produce only a single axes, add_subplot(111) would be used (First plot on a 1 by 1 subplot grid). (In newer matplotlib versions, add_subplot() without any arguments is possible as well.)
The advantage of this method is that matplotlib takes care of the exact positioning. By default add_subplot(111) would produce an axes positioned at [0.125,0.11,0.775,0.77] or similar, which already leaves enough space around the axes for the title and the (tick)labels. However, this position may also change depending on other elements in the plot, titles set, etc.
It can also be adjusted using pyplot.subplots_adjust(...) or pyplot.tight_layout().
In most cases, add_subplot would be the prefered method to create axes for plots on a canvas. Only in cases where exact positioning matters, add_axes might be useful.
Example
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (5,3)
fig = plt.figure()
fig.add_subplot(241)
fig.add_subplot(242)
ax = fig.add_subplot(223)
ax.set_title("subplots")
fig.add_axes([0.77,.3,.2,.6])
ax2 =fig.add_axes([0.67,.5,.2,.3])
fig.add_axes([0.6,.1,.35,.3])
ax2.set_title("random axes")
plt.tight_layout()
plt.show()
Alternative
The easiest way to obtain one or more subplots together with their handles is plt.subplots(). For one axes, use
fig, ax = plt.subplots()
or, if more subplots are needed,
fig, axes = plt.subplots(nrows=3, ncols=4)
The initial question
In the initial question an axes was placed using fig.add_axes([0,0,1,1]), such that it sits tight to the figure boundaries. The disadvantage of this is of course that ticks, ticklabels, axes labels and titles are cut off. Therefore I suggested in one of the comments to the answer to use fig.add_subplot as this will automatically allow for enough space for those elements, and, if this is not enough, can be adjusted using pyplot.subplots_adjust(...) or pyplot.tight_layout().
The answer by #ImportanceOfBeingErnest is great.
Yet in that context usually one want to generate an axes for a plot and add_axes() has too much overhead.
So one trick is, as in the answer of #ImportanceOfBeingErnest, is to use add_subplot(111).
Yet more elegant alternative and simple would be:
hAx = plt.figure(figsize = (10, 10)).gca()
If you want 3D projection you can pass any axes property. For instance the projection:
hAx = plt.figure(figsize = (16, 10)).gca(projection = '3d')
I have a very customized subplot set up.
fig = plt.figure(figsize=(12, 10))
gs = fig.add_gridspec(nrows=2, ncols=2, width_ratios=[3, 1])
ax = fig.add_subplot(gs[:, 0])
ax3 = fig.add_subplot(gs[-1, -1])
ax4=fig.add_subplot(gs[0, 1])
This sets up 3 slots for plotting: one that takes up half the space on the left, and two smaller ones on the right. However, I only want the bottom right to actually be a plot. I want the top right to be the space where the legend for the larger plot on the left to go. I could just use the axes from ax to do this, but that shifts the whole plotting space off. Instead I thought of trying to just create ax4 and place the ax legend there.
lines = []
labels = []
for ax in fig.get_axes():
ln, la = ax.get_legend_handles_labels()
lines.extend(ln)
labels.extend(la)
legend = ax4.legend(lines, labels, labelspacing=0.1, loc=(-0.3,0.6), fontsize='xx-large')
fig.tight_layout()
This puts the legend exactly where I want it, but the blank figure shows up, which I don't want. Is it possible to accomplish what I want using this method? If not, what is my alternative? Picture below to better understand.
You can use ax4.axis('off') to make axis 4 invisible if you want to stick to your approach.
However, I don't see why you don't just skip creating axis 4 and just use fig.legend() instead of ax.legend(). Then the legend is placed outside the axis and you can then control the exact position just as you already did with the loc keyword.
I'm trying to set y-axis limit for a certain subplot using plt.ylim. (in my example, the plot on ax1)
However, no matter where I put the command plt.ylim((10,20)), it only works on the last subplot (in the following example, it is the plot on ax2).
fig, (ax1,ax2) = plt.subplots(2,1)
x=range(1,100)
y=range(1,100)
plt.ylim((10,20))
ax1.plot(x,y)
ax2.plot(x,y)
Only ax2 will be limited and ax1 will still be in the original range.
fig, (ax1,ax2) = plt.subplots(2,1)
x=range(1,100)
y=range(1,100)
ax1.plot(x,y)
ax2.plot(x,y)
plt.ylim((10,20))
screenshot for the result
Running the two blocks of code will produce the same result. I know I can also use other methods like plt.setp(ax1, ylim=[10,20]). But I'd like to know how to use plt.ylim properly.
Thank you very much in advance!
I am new to matplotlib, and I am finding it very confusing. I have spent quite a lot of time on the matplotlib tutorial website, but I still cannot really understand how to build a figure from scratch. To me, this means doing everything manually... not using the plt.plot() function, but always setting figure, axis handles.
Can anyone explain how to set up a figure from the ground up?
Right now, I have this code to generate a double y-axis plot. But my xlabels are disappearing and I dont' know why
fig, ax1 = plt.subplots()
ax1.plot(yearsTotal,timeseries_data1,'r-')
ax1.set_ylabel('Windspeed [m/s]')
ax1.tick_params('y',colors='r')
ax2 = ax1.twinx()
ax2.plot(yearsTotal,timeseries_data2,'b-')
ax2.set_xticks(np.arange(min(yearsTotal),max(yearsTotal)+1))
ax2.set_xticklabels(ax1.xaxis.get_majorticklabels(), rotation=90)
ax2.set_ylabel('Open water duration [days]')
ax2.tick_params('y',colors='b')
plt.title('My title')
fig.tight_layout()
plt.savefig('plots/my_figure.png',bbox_inches='tight')
plt.show()
Because you are using a twinx, it makes sense to operate only on the original axes (ax1).
Further, the ticklabels are not defined at the point where you call ax1.xaxis.get_majorticklabels().
If you want to set the ticks and ticklabels manually, you can use your own data to do so (although I wouldn't know why you'd prefer this over using the automatic labeling) by specifying a list or array
ticks = np.arange(min(yearsTotal),max(yearsTotal)+1)
ax1.set_xticks(ticks)
ax1.set_xticklabels(ticks)
Since the ticklabels are the same as the tickpositions here, you may also just do
ax1.set_xticks(np.arange(min(yearsTotal),max(yearsTotal)+1))
plt.setp(ax1.get_xticklabels(), rotation=70)
Complete example:
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
yearsTotal = np.arange(1977, 1999)
timeseries_data1 = np.cumsum(np.random.normal(size=len(yearsTotal)))+5
timeseries_data2 = np.cumsum(np.random.normal(size=len(yearsTotal)))+20
fig, ax1 = plt.subplots()
ax1.plot(yearsTotal,timeseries_data1,'r-')
ax1.set_ylabel('Windspeed [m/s]')
ax1.tick_params('y',colors='r')
ax1.set_xticks(np.arange(min(yearsTotal),max(yearsTotal)+1))
plt.setp(ax1.get_xticklabels(), rotation=70)
ax2 = ax1.twinx()
ax2.plot(yearsTotal,timeseries_data2,'b-')
ax2.set_ylabel('Open water duration [days]')
ax2.tick_params('y',colors='b')
plt.title('My title')
fig.tight_layout()
plt.show()
Based on your code, it is not disappear, it is set (overwrite) by these two functions:
ax2.set_xticks(np.arange(min(yearsTotal),max(yearsTotal)+1))
ax2.set_xticklabels(ax1.xaxis.get_majorticklabels(), rotation=90)
set_xticks() on the axes will set the locations and set_xticklabels() will set the xtick labels with list of strings labels.
What is the most pythonic way to plot multiple lineswith very different scales in the same graph with matplotlib? I know can create subplots, but I am not really sure they will give me the best visualization. I don't really care about coloring, legends or any other intricacies at this point.
If you only need two scales then you can simple use twinx and/or twiny
fig, ax = plt.subplots(1, 1)
x = np.arange(11)
ax.plot(x, 'r')
ax2 = ax.twinx()
ax2.plot(x ** 2, 'g')
plt.draw()
I you need more than two see matplotlib: adding second axes() with transparent background? or look into parasitic axes.