Is there any possibility to get lines and points into a legend text in matplotlib?
I had something in mind like the following
x=np.linspace(0,10,100)
ys=np.sin(x)
yc=np.cos(x)
pl.plot(x,ys,'--',label='sin')
pl.plot(x,yc,':',label='derivative of --')
pl.legend()
pl.show()
except that instead of the -- there should be the same symbol with the corresponding color just as in front of the legend label sin.
After reading around in the matplotlib source code I finally found a solution that works perfect for me and that does not need any position tweaking etc. as it used matplotlibs internal V- and HPackers.
import numpy as np
import pylab as pl
x=np.linspace(0,10,100)
ys=np.sin(x)
yc=np.cos(x)
pl.plot(x,ys,'--',label='sin')
pl.plot(x,yc,':',label='derivative of')
leg=pl.legend()
# let the hacking begin
legrows = leg.get_children()[0].get_children()[1]\
.get_children()[0].get_children()
symbol = legrows[0].get_children()[0]
childs = legrows[1].get_children().append(symbol)
pl.show()
The result looks as follows:
This is a little bit of a hack, but it accomplishes your goal and places all of the pieces (i.e. the legend and the text) on the plot in the appropriate order.
import pylab
pl.plot(x,ys,'--',label='sin', color='green')
pl.plot(x,yc,':',label='derivative of --',color='blue')
line1= pylab.Line2D(range(10), range(10), marker='None', linestyle='--',linewidth=2.0, color="green")
line2= pylab.Line2D(range(10), range(10), marker='None', linestyle=':',linewidth=2.0, color="blue")
leg = pl.legend((line1,line2),('sin','derivative of '),numpoints=1, loc=1)
pylab.text(9.4, 0.73, '- -', color='green')
leg.set_zorder(2)
pl.show()
Instead of relying on the default colors for the lines, I set them such that they can be referenced specifically in the legend. There are extra spaces left in the text for 'the derivative' for the second line in the legend, so we can place text (aka corresponding symbol/color of the sin line) on top of it. Then you specify the symbol/color of the text and place it such that it lines up with the text in the legend. Finally you specify the order, via zorder, to set the text on top.
Related
I am working on a software that solve an engineering problem. The software should printout the calculation for the user in a pretty mathematical expression format. I used Matplotlib,Latex and Sympy in PyQt and succeeded to do everything I wanted except for displaying the values of the variables in the form of a fracture (please see the picture below to understand what I mean). Also, I would like to know how to control the font size and style of the latex text(see the picture). below is a part of the code.
See this picture
def Calculate(self):
plt.rc('mathtext', fontset='cm')
self.right_column_stiffness = int(self.lineEdit_3.text())
self.left_column_stiffness = int(self.lineEdit_4.text())
self.beam_load = int(self.lineEdit_4.text())
w=self.beam_load*(1000/12)
g=386.1
self.mass = w/g
formula=r'm=\frac{w}{g}='
self.result_figure.text(.05, .85, r'${}$'.format(formula+latex(w)), fontsize=20)
[enter image description here][1]
self.result_canvas.draw()
There are two questions you have asked:
The font size of latex representation of fractions can be changed in two ways. First is to surround the fraction latex code with \displaystyle block.
formula = r'm={\displaystyle\frac{w}{g}}'
Other option is to change '${}$' to \[{}\] in the following line.
self.result_figure.text(.05, .85, r'\[{}\]'.format(formula+latex(w)), fontsize=20)
In order to print the values contained in variables as a fraction rather than the final answer, you will have to manually construct another latex text.
value = r'${{\displaystyle\frac{{{0}}}{{{1}}}}}$'.format(w,g)
Here is my example code:
import matplotlib.pyplot as plt
from matplotlib import rc # Added these two lines
rc('text', usetex=True)
formula = r'm=\frac{{w}}{{g}} = \frac{{{0}}}{{{1}}}'.format(100,20)
plt.plot( [0,1,2,3], [0,1,2,3], '.')
plt.text(1,1,r'\[{}\]'.format(formula),fontsize=20)
plt.show()
Which gives the following result
EDIT: ImportanceOfBeingErnest provided the answer, however I am still inviting you all to explain, why is savefig logic different from animation logic.
I want to make a video in matplotlib. I went through manuals and examples and I just don't get it. (regarding matplotlib, I always copy examples, because after five years of python and two years of mathplotlib I still understand 0.0% of matplotlib syntax)
After half a dozen hours here is what I came up to. Well, I get empty video. No idea why.
import os
import math
import matplotlib
matplotlib.use("Agg")
from matplotlib import pyplot as plt
import matplotlib.animation as animation
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
numb=100
temp=[0.0]*numb
cont=[0.0]*numb
for i in range(int(4*numb/10),int(6*numb/10)):
temp[i]=2
cont[i]=2
fig = plt.figure()
plts=fig.add_subplot(1,1,1)
plts.set_ylim([0,2.1])
plts.get_xaxis().set_visible(False)
plts.get_yaxis().set_visible(False)
ims = []
for i in range(1,10):
line1, = plts.plot(range(0,numb),temp, linewidth=1, color='black')
line2, = plts.plot(range(0,numb),cont, linewidth=1, color='red')
# savefig is here for testing, works perfectly!
# fig.savefig('test'+str(i)+'.png', bbox_inches='tight', dpi=300)
ims.append([line1,line2])
plts.lines.remove(line1)
plts.lines.remove(line2)
for j in range(1,10):
tempa=0
for k in range (1,numb-1):
tempb=temp[k]+0.51*(temp[k-1]-2*temp[k]+temp[k+1])
temp[k-1]=tempa
tempa=tempb
temp[numb-1]=0
for j in range(1,20):
conta=0
for k in range (1,numb-1):
contb=cont[k]+0.255*(cont[k-1]-2*cont[k]+cont[k+1])
cont[k-1]=conta
conta=contb
cont[numb-1]=0
im_ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,blit=True)
im_ani.save('im.mp4', writer=writer)
Can someone help me with this?
If you want to have a plot which is not empty, the main idea would be not to remove the lines from the plot.
That is, delete the two lines
plts.lines.remove(line1)
plts.lines.remove(line2)
If you delete these two lines the output will look something like this
[Link to orginial size animation]
Now one might ask, why do I not need to remove the artist in each iteration step, as otherwise all of the lines would populate the canvas at once?
The answer is that the ArtistAnimation takes care of this. It will only show those artists in the supplied list that correspond to the given time step. So while at the end of the for loop you end up with all the lines drawn to the canvas, once the animation starts they will all be removed and only one set of artists is shown at a time.
In such a case it is of course not a good idea to use the loop for saving the individual images as the final image would contain all of the drawn line at once,
The solution is then either to make two runs of the script, one for the animation, and one where the lines are removes in each timestep. Or, maybe better, use the animation istself to create the images.
im_ani.save('im.png', writer="imagemagick")
will create the images as im-<nr>.png in the current folder. It will require to have imagemagick installed.
I'm trying here to answer the two questions from the comments:
1. I have appended line1 and line2 before deleting them. Still they disappeared in the final result. How come?
You have appended the lines to a list. After that you removed the lines from the axes. Now the lines are in the list but not part of the axes. When doing the animation, matplotlib finds the lines in the list and makes them visible. But they are not in the axes (because they have been removed) so the visibility of some Line2D object, which does not live in any axes but only somewhere in memory, is changed. But that isn't reflected in the plot because the plot doesn't know this line any more.
2. If I understand right, when you issue line1, = plts.plot... command then the line1 plot object is added to the plts graph object. However, if you change the line1 plot object by issuing line1, = plts.plot... command again, matplotlib does change line1 object but before that saves the old line1 to the plts graph object permanently. Is this what caused my problem?
No. The first step is correct, line1, = plts.plot(..) adds a Line2D object to the axes. However, in a later loop step line1, = plts.plot() creates another Line2D object and puts it to the canvas. The initial Line2D object is not changed and it doesn't know that there is now some other line next to it in the plot. Therefore, if you don't remove the lines they will all be visible in the static plot at the end.
I would like to position a text box with keyword arguments like those used with legend 'loc' option, i.e. 'upper left', 'upper right', 'lower right', 'lower left'.
The basic purpose is to align the text box with legend.
I found a suggestion here : automatically position text box in matplotlib but it still uses coordinates with which I have to play to get what I want, especially if I want to put it on the right of the plot area depending on the length of the text put in the box. Unless I can set one of the right corner of the text box as the reference for coordinates.
You can do this with matplotlib's AnchoredText. As shown here:
automatically position text box in matplotlib
In brief:
from matplotlib.offsetbox import AnchoredText
anchored_text = AnchoredText("Test", loc=2)
ax.plot(x,y)
ax.add_artist(anchored_text)
Each loc number corresponds to a position within the axes, for example loc=2 is "upper left". The full list of positions is given here : http://matplotlib.org/api/offsetbox_api.html
How about setting the location of the text relative to the legend? The trick is, to find the location of the legend you have to draw it first then get the bbox. Here's an example:
import matplotlib.pyplot as plt
from numpy import random
# Plot some stuff
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(random.rand(10))
# Add a legend
leg = ax.legend('line', loc = 'upper right')
# Draw the figure so you can find the positon of the legend.
plt.draw()
# Get the Bbox
bb = leg.legendPatch.get_bbox().inverse_transformed(ax.transAxes)
# Add text relative to the location of the legend.
ax.text(bb.x0, bb.y0 - bb.height, 'text', transform=ax.transAxes)
plt.show()
On the other hand, if you only need to define the location of the text from the right you can set the horizontal alignment to right like this:
plt.text(x, y, 'text', ha = 'right')
I am using the following example Example to create two polar contour subplots. When I create as the pdf there is a lot of white space which I want to remove by changing figsize.
I know how to change figsize usually but I am having difficulty seeing where to put it in this code example. Any guidance or hint would be greatly appreciated.
Many thanks!
import numpy as np
import matplotlib.pyplot as plt
#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))
#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
plt.show()
Another way to do this would be to use the figsize kwarg in your call to plt.subplots.
fig, ax = plt.subplots(figsize=(6,6), subplot_kw=dict(projection='polar')).
Those values are in inches, by the way.
You can easily just put plt.figsize(x,y) at the beginning of the code, and it will work. plt.figsize changes the size of all future plots, not just the current plot.
However, I think your problem is not what you think it is. There tends to be quite a bit of whitespace in generated PDFs unless you change options around. I usually use
plt.savefig( 'name.pdf', bbox_inches='tight', pad_inches=0 )
This gives as little whitespace as possible. bbox_inches='tight' tries to make the bounding box as small as possible, while pad_inches sets how many inches of whitespace there should be padding it. In my case I have no extra padding at all, as I add padding in whatever I'm using the figure for.
I have a couple of lines and I want to show a legend. The problem is, I can't use different styles (--, :, -.) because there are too few of them, and I can't use markers (+, *, etc.) because I need them to show some points on the lines.
So the best idea I've come up with is to use numbers. But I can't figure how I can create legends with numbers. I can even draw numbers near lines myself (to place them in the best position), but how can I then draw a legend with the numbers?
I.e. instead of:
-- H
-.- Li
I'd like something like:
1 H
2 Li
Perhaps a little Latex thrown into the mix?
#In which we make a legend; not with lines, but numbers!
import pylab as pl
pl.rc('text', usetex=True)
pl.figure(1)
pl.clf()
ax = pl.subplot(111)
pl.plot(range(0,10), 'k', label = r'\makebox[25]{1\hfill}Bla')
pl.plot(range(1,11), 'k', label = r'\makebox[25]{12\hfill}Bla12')
lgd = pl.legend(handlelength = -0.4)
for k in lgd.get_lines():
k.set_linewidth(0)
pl.draw()
pl.show()
The numbers/labels are aligned by using \makebox with specific width and \hfill to take up the space not used by your labels. Numbers are not automatic, but if you use a loop to draw your lines then you could add a counter to keep track of the numbers.
Don't know if this is part of your requirement, but the lines are removed by setting their linewidth to 0 and making the space reserved in the legend negative. Couldn't find a neater way of doing this as I believe a legend is always meant to show a line (e.g. you can't set numpoints to 0).
You could of course also just add some text in the right spot in your plot and not use a legend at all.