How to view legend next to matplotlib? - python

I've got a plot in matplotlib, which has a lot of lines. I've got a legend which is therefore rather extensive and I placed it next to my plot using the following code:
fontP = FontProperties()
fontP.set_size('small')
plt.legend(variablelist, loc=0, prop = fontP, bbox_to_anchor=(1.0, 1.0))
plt.savefig(filename+'.png')
The result is as follows:
As you can see however, the legend is cut off on the right. Is there a way that I can create more space on the right side of the image so that I can see the full legend?
All tips are welcome!
In response to #mmgp I posted the following code below. As you can see by his answer I forgot to add bbox_inches='tight' to the savefig part. So for future readers to have a fully working code I just added the bbox_inches='tight' in the code below, which makes it work perfectly well.. :) :
from random import random
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
mylist = []
for a in range(10):
mylist.append([])
for b in range(10):
mylist[a].append(random())
x = range(len(mylist))
for enum, i in enumerate(mylist):
plt.plot(x, mylist[enum], label='label_'+str(enum))
plt.grid(b=True)
fontP = FontProperties()
fontP.set_size('small')
variablesList = []
for i in range(10):
variablesList.append('label_'+str(i))
legenda = plt.legend(variablesList, loc=0, prop = fontP, bbox_to_anchor=(1.0, 1.0))
plt.savefig('testplot.png', bbox_extra_artists=[legenda], bbox_inches='tight')

Almost there now, just add a new parameter in savefig: bbox_inches = 'tight'. That makes matplotlib figure out the needed size for your plot.

Related

Title font in subplots with axes.title.set_text

I have intention in making multiple subplot to present my results. I used subplots from matplotlib. I have a problem with text sizes. As you can see in the trivial code here. In the plt.title documentation it says title(label, fontdict=None, loc='center', pad=None, **kwargs)
import random
from matplotlib.pyplot import figure, plot, xlabel, ylabel, legend, close, subplots, title, savefig, get_current_fig_manager, show, pause, clf
x = []
for i in range(10):
x.append(random.random()*i)
y_1 = []
for i in range(10):
y_1.append(random.random()*i)
y_2 = []
for i in range(10):
y_2.append(random.random()*i)
fig, ax = subplots(1, 2, squeeze = False, figsize = (10,10))
ax[0,1].title.set_text('y_1', fontdict = {'font.size':22})
ax[0,1].plot(x,y_1)
ax[0,1].set_xlabel('x')
ax[0,1].set_ylabel('y_1')
ax[0,0].title.set_text('y_2', fontdict = {'font.size':22})
ax[0,0].plot(x,y_2)
ax[0,0].set_xlabel('x')
ax[0,0].set_ylabel('y_2')
but if I run this code I get an error TypeError: set_text() got an unexpected keyword argument 'fontdict'
am I using the wrong command.
This is really just a minor issue:
To set the title of a specific axes you should use the set_title method of the axes. Using plt.title sets the title of the current axes instance.
Basically replace your ax[0,0].title.set_text with ax[0,0].set_title and you are good to go!
You can also simply use fontsize=22 directly , as in
ax[0,1].set_title('y_1', fontsize=22)

How to add legend/label in python animation

I want to add a legend in a python animation, like the line.set_label() below. It is similar to plt.plot(x,y,label='%d' %*variable*).
However, I find that codes do not work here. The animation only shows lines changing but no label or legend available. How can I fix this problem?
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(0, 100))
N = 3
lines = [plt.plot([], [])[0] for _ in range(N)]
def init():
for line in lines:
line.set_data([], [])
return lines
def animate(i):
for j,line in enumerate(lines):
line.set_data([0, 2], [10*j,i])
line.set_label('line %d, stage %d'%(j,i))
return lines
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
you must return the legend in your animation function for it to be rendered.
Try this, instead :
legend = plt.legend()
def animate(i):
for j,line in enumerate(lines):
line.set_data([0, 2], [10*j,i])
line.set_label('line %d, stage %d'%(j,i))
legend.remove()
legend = plt.legend()
return lines + [legend]
You should also include the same code in your init function, init is used when resizing the window, otherwise the legend will disappear when resizing
I'm no expert on matplotlib at all, but in the Double Pendulum animation they display texts which changes, and this leads to some variations which can help you.
To get legends with the actual color of the lines, you can either change the initial setting lines to:
lines = [plt.plot([], [], label = 'line {}'.format(i))[0] for i in range(N)]
or add a line.set_label() to the for loop in the init() function. Both these seem to work as expected. At least if you add plt.legend(loc="upper left") right before plt.show().
However the set_label doesn't work within the animate() function, but according to the linked animation you can use specific text fields added to the animation, and that seems to work nicely. Add the following code after initialisation of lines:
texts = [ax.text(0.80, 0.95-i*0.05, '', transform=ax.transAxes) for i in range(N)]
And change animate() to be:
def animate(i):
for j in range(N):
lines[j].set_data([0, 2], [10*j,i]) #, label="hei {}".format(i))
texts[j].set_text('line %d, stage %d'%(j,i))
return lines
This places the text close to the upper right corner, and is updated for each animation step. Since the lines still have their legend displayed, you possibly simplify into one text only displaying the stage. But I leave the fine tuning of messages to your discretion.
Addendum: Extend Line2D
Another alternative could possibly be to extend lines.Line2D and use these lines in your animation, something similar to this article. Not sure if this would work with animation, but if you can't get the above to work, this might be worth a try.
You can try this minimum working example below. The handle of legend hlegend consist of handles of text htext, so we can update htext content in the hf_frame.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
N0 = 20
xdata = np.linspace(0, 2*np.pi, 100)
omega = np.linspace(1, 4, N0)
ydata = np.sin(omega[:,np.newaxis]*xdata)
hline0, = ax.plot(xdata, ydata[0], label=f'omega={omega[0]:.3f}')
hlegend = ax.legend(loc='upper right')
def hf_frame(ind0):
hline0.set_data(xdata, ydata[ind0])
label_i = f'{omega[ind0]:.3f}'
# hline0.set_label(label_i) #doesn't help
htext = hlegend.get_texts()[0]
htext.set_text(label_i)
return hline0,htext
ani = matplotlib.animation.FuncAnimation(fig, hf_frame, frames=N0, interval=200)
plt.show()

Matplotlib: Saved files in a loop aren't the same as in show()

My program shows the correct graph in the plt.show() pop up but not in the fig.savefig one. I'm quite new to python so apologies if it is something simple.
I'm using python 2.7.10, windows (10).
import numpy as np
import matplotlib.pyplot as plt
data = np.genfromtxt('strike_details.txt') #, skip_header= 0
header= 3
information=10000
width = 5
files = 16
types = 4
length = information + header
frames = data[header:length,0]
fig= plt.figure()
plt.grid(True)
for i in range(0,int(files)):
density=data[(header+i*length):(length+i*length),4]
plt.plot(frames,density, label=data[i*length+1][2])
for j in range (0,files/types):
if i==(types*(j+1)-1):
plt.legend(loc='best')
plt.xlabel('$Frames$', fontsize=22)
plt.ylabel('$Density$', fontsize=22)
fig.savefig(str(data[j*length+1][0])+'_'+str(data[j*length+1][1])+'_'+str(data[j*length+1][2])+'.png',format='png', dpi=fig.dpi)
plt.show()
plt.clf()
The program produces four files with different file names but they're all of the first group you see in the plt.show pop up.
If I missed out anything important let me know.
Thanks,
Lio
I think this is due to mixing the API-style and interactive-styles of matplotlib. When you call plt.show() the link between the active figure and fig is broken, and so you continue to output the first figure you created. I can reproduce this problem with this minimal example:
import matplotlib.pyplot as plt
fig = plt.figure()
for n in range(0,10):
plt.plot(list(range(0,n)))
fig.savefig('test%d.png' % n)
plt.show()
plt.clf()
If you remove the show() the issue goes away.
The correct way to do this is to access the current interactive figure via plt.gcf():
plt.gcf().savefig(...)
Alternatively, you can workaround it by recreating the figure object on each loop:
for i in range(0,int(files)):
fig= plt.figure()
plt.grid(True)
...

matplotlib plotting in loop, removing colorbar but whitespace remains

My code is something (roughly) like this:
UPDATE: I've redone this with some actual mock-up code that reflects my general problem. Also, realized that the colorbar creation is in the actual loop as otherwise there's nothing to map it to. Sorry for the code before, typed it up in frantic desperation at the very end of the workday :).
import numpy
import matplotlib as mplot
import matplotlib.pyplot as plt
import os
#make some mock data
x = np.linspace(1,2, 100)
X, Y = np.meshgrid(x, x)
Z = plt.mlab.bivariate_normal(X,Y,1,1,0,0)
fig = plt.figure()
ax = plt.axes()
'''
Do some figure-related stuff that take up a lot of time,
I want to avoid having to do them in the loop over and over again.
They hinge on the presence of fig so I can't make
new figure to save each time or something, I'd have to do
them all over again.
'''
for i in range(1,1000):
plotted = plt.plot(X,Y,Z)
cbar = plt.colorbar(ax=ax, orientation = 'horizontal')
plt.savefig(os.path.expanduser(os.path.join('~/', str(i))))
plt.draw()
mplot.figure.Figure.delaxes(fig, fig.axes[1]) #deletes but whitespace remains
'''
Here I need something to remove the colorbar otherwise
I end up with +1 colorbar on my plot at every iteration.
I've tried various things to remove it BUT it keeps adding whitespace instead
so doesn't actually fix anything.
'''
Has anyone come across this problem before and managed to fix it? Hopefully this is enough
for an idea of the problem, I can post more code if needed but thought it'd be less of a clutter if I just give an overview example.
Thanks.
colorbar() allows you explicitly set which axis to render into - you can use this to ensure that they always appear in the same place, and not steal any space from another axis. Furthermore, you could reset the .mappable attribute of an existing colorbar, rather than redefine it each time.
Example with explicit axes:
x = np.linspace(1,2, 100)
X, Y = np.meshgrid(x, x)
Z = plt.mlab.bivariate_normal(X,Y,1,1,0,0)
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.7])
ax2 = fig.add_axes([0.1,0.85,0.8,0.05])
...
for i in range(1,5):
plotted = ax1.pcolor(X,Y,Z)
cbar = plt.colorbar(mappable=plotted, cax=ax2, orientation = 'horizontal')
#note "cax" instead of "ax"
plt.savefig(os.path.expanduser(os.path.join('~/', str(i))))
plt.draw()
I had a very similar problem, which I finally managed to solve by defining a colorbar axes in a similar fashion to:
Multiple imshow-subplots, each with colorbar
The advantage compared to mdurant's answer is that it saves defining the axes location manually.
import matplotlib.pyplot as plt
import IPython.display as display
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pylab import *
%matplotlib inline
def plot_res(ax,cax):
plotted=ax.imshow(rand(10, 10))
cbar=plt.colorbar(mappable=plotted,cax=cax)
fig, axarr = plt.subplots(2, 2)
cax1 = make_axes_locatable(axarr[0,0]).append_axes("right", size="10%", pad=0.05)
cax2 = make_axes_locatable(axarr[0,1]).append_axes("right", size="10%", pad=0.05)
cax3 = make_axes_locatable(axarr[1,0]).append_axes("right", size="10%", pad=0.05)
cax4 = make_axes_locatable(axarr[1,1]).append_axes("right", size="10%", pad=0.05)
# plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.3, hspace=0.3)
N=10
for j in range(N):
plot_res(axarr[0,0],cax1)
plot_res(axarr[0,1],cax2)
plot_res(axarr[1,0],cax3)
plot_res(axarr[1,1],cax4)
display.clear_output(wait=True)
display.display(plt.gcf())
display.clear_output(wait=True)

Matplotlib make tick labels font size smaller

In a matplotlib figure, how can I make the font size for the tick labels using ax1.set_xticklabels() smaller?
Further, how can one rotate it from horizontal to vertical?
There is a simpler way actually. I just found:
import matplotlib.pyplot as plt
# We prepare the plot
fig, ax = plt.subplots()
# We change the fontsize of minor ticks label
ax.tick_params(axis='both', which='major', labelsize=10)
ax.tick_params(axis='both', which='minor', labelsize=8)
This only answers to the size of label part of your question though.
To specify both font size and rotation at the same time, try this:
plt.xticks(fontsize=14, rotation=90)
Please note that newer versions of MPL have a shortcut for this task. An example is shown in the other answer to this question: https://stackoverflow.com/a/11386056/42346
The code below is for illustrative purposes and may not necessarily be optimized.
import matplotlib.pyplot as plt
import numpy as np
def xticklabels_example():
fig = plt.figure()
x = np.arange(20)
y1 = np.cos(x)
y2 = (x**2)
y3 = (x**3)
yn = (y1,y2,y3)
COLORS = ('b','g','k')
for i,y in enumerate(yn):
ax = fig.add_subplot(len(yn),1,i+1)
ax.plot(x, y, ls='solid', color=COLORS[i])
if i != len(yn) - 1:
# all but last
ax.set_xticklabels( () )
else:
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontsize(14)
# specify integer or one of preset strings, e.g.
#tick.label.set_fontsize('x-small')
tick.label.set_rotation('vertical')
fig.suptitle('Matplotlib xticklabels Example')
plt.show()
if __name__ == '__main__':
xticklabels_example()
Alternatively, you can just do:
import matplotlib as mpl
label_size = 8
mpl.rcParams['xtick.labelsize'] = label_size
Another alternative
I have two plots side by side and would like to adjust tick labels separately.
The above solutions were close however they were not working out for me. I found my solution from this matplotlib page.
ax.xaxis.set_tick_params(labelsize=20)
This did the trick and was straight to the point. For my use case, it was the plot on the right that needed to be adjusted. For the plot on the left since I was creating new tick labels I was able to adjust the font in the same process as seting the labels.
ie
ax1.set_xticklabels(ax1_x, fontsize=15)
ax1.set_yticklabels(ax1_y, fontsize=15)
thus I used for the right plot,
ax2.xaxis.set_tick_params(labelsize=24)
ax2.yaxis.set_tick_params(labelsize=24)
A minor subtlety... I know... but I hope this helps someone :)
Bonus points if anyone knows how to adjust the font size of the order of magnitude label.
plt.tick_params(axis='both', which='minor', labelsize=12)
In current versions of Matplotlib, you can do axis.set_xticklabels(labels, fontsize='small').
The following worked for me:
ax2.xaxis.set_tick_params(labelsize=7)
ax2.yaxis.set_tick_params(labelsize=7)
The advantage of the above is you do not need to provide the array of labels and works with any data on the axes.
For smaller font, I use
ax1.set_xticklabels(xticklabels, fontsize=7)
and it works!
You can also change label display parameters like fontsize with a line like this:
zed = [tick.label.set_fontsize(14) for tick in ax.yaxis.get_major_ticks()]

Categories

Resources