How to remove space only at the top of the plot - matplotlib - python

I've seen many posts and answers online trying to answer this question.
However using bbox_inches = 'tight' the legend disappears.
This is one of my figures:
Since I have the legend outside the plot frame, I would like to remove only the top and bottom white space.
Anyone knows how to remove at least the top white space?
Thanks a lot!

Have you tried using subplots_adjust()? See, for example, the answer of #DaveP to this question: Reduce left and right margins in matplotlib plot
Also, look at the answer by #Tian Chu to the same question.
EDIT: This works for me:
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2,3],[5,6,7],'gs-')
plt.subplots_adjust(top=0.99, right=0.99)
plt.show()

I usually don't use the bbox_inches = 'tight' feature, since it doesn't work very reliably, as you already found out. I'd rather produce a PDF with bounds and then crop them using external tools. To do this seamless from python, I use
os.system('pdfcrop %s %s &> /dev/null &'%(pdf_in, pdf_out))
Here, pdf_in is the PDF you produced from matplotlib and pdf_out will be your final result.

Related

How to make plots customizable in python

I have two issues with my python plot that would be grateful if anyone could help me with:
1- I wonder if it is possible in python to have the option for the plots after display to add horizontal or vertical lines, so that these new lines could be added, moved or deleted without the need to run the code again.
to say it more clearly, I am looking for additional features that adding them does not need to change the code and they only enable me to manually draw on the already plotted image.
2- I want to plot a very large image in the real size, So that I need to add the horizontal and vertical slide bars to be able to scroll up/down or left/right in the plot?
I need to combine these two ability for my project, can someone help me with that?
1- You can't physically draw on it, but you can make a plot in matplotlib interactive as follows:
import matplotlib.pyplot as plt
plt.ion() # turns on interactive mode
fig = plt.figure()
ax = fig.add_subplot()
plt.ylim(-10, 10)
plt.xlim(0, 10)
while True:
plt.axhline(float(input("number")))
fig.canvas.draw()
fig.canvas.flush_events() # draws
This program allows you to create horizontal lines based on user input.
I think you can solve 2 with tkinter, but that would be pretty difficult. There might also an easier way. See this stack overflow question for an example of an interactive plot in tkinter. I believe this plot can be made bigger and scrollable, but I am not sure.

"Invert" Axis on Matplotlib / Seaborn

Good evening/morning/evening !
I am only a leisure programmer so I appologise if this question has been answered on here before under a different title, I didn't know what to search for.
If you see below I have plotted two graphs, one a 2d and the other a 3d using matplotlib.
My issue is that I wish for (0,0) to be in the bottom left corner and a step to the right to be +1 and a step upwards to be -1. Instead of having x increase and y decrease. If it is needed I will post the entire code for these plots but they have both been done conventionally with seaborn.heatmap(z) and ax.plot_surface(x,y,z).
Also I am using the following line I found on here: ax = fig.add_subplot(2, 1, 1)
Could someone please explain the parameters of this function to me I am struggling to understand what they mean.
Any help is greatly appreciated and again I apologise if this has been posted before :)
In matplotlib:
If you want to invert the x-axis:
ax.invert_xaxis()
If you want to invert the y-axis:
ax.invert_yaxis()
If you want to invert the z-axis:
ax.invert_zaxis()
I'm pretty sure that these functions will work in seaborn as well, since it is built on top of matplotlib!

SVG rendering issues using iPython inline plots

when I use inline plots in iPython (QtConsole), the first plot looks (more or less) fine, but then it gets weirder and weirder. When I plot something several times (so plot, see it displayed, plot again, see output etc.), it looks like it is being overlaid with the skewed previous picture. So after plotting a diagonal line (x=y) 4 times in a row I get something like this
If i right click and export it as svg everything looks good
(Exported PNG picture remains wrecked as the first one).
I guess the problem is similar to https://github.com/ipython/ipython/issues/1866, but I didn't got the upshot of the discussion (it got too technical and complicated for me to follow).
Is there any solution or work around for this issue?
I'm using
python 2.7
matplotlib 1.4.1
IPython 2.1.0
Here is a working example:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
ax.plot(a,a)
ax.axis('off')
if you remove plt.axis('off') line, weird things happen only outside of the axis box.
P.S. Originally I encountered this problem in connection with drawing graphs with networkx. If I use draw from networkx this problem does not occur. If I use draw_networkx, same as described above happens. That might point to the core of the problem... I'm trying to figure out what line of code makes one work better than the other...
After tinkering around with the draw and draw_networkx functions from networkx module, I found the workaround which makes the difference between draw and draw_networkx in this case.
Adding fig.set_facecolor('w') overlays whatever is in the background, so the new plots are started with a white sheet (but not a blank one, I guess).
So new working example is:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
fig.set_facecolor('w')
ax.plot(a,a)
ax.axis('off')

Python- Connecting Lines with plot_date in Matplotlib

I am using matplotlib to graph out some data in which takes time over a time, therefore I have to use plot_date in order to plot my lines. But for some reason Plot_Date and Plot have completely different formatting as far as connecting lines.
Here is what It looks like when using plot(x,y)
Using plot(x,y,'bo') or plot_date(x,y,'bo')
Plot_date(x,y) looks like that ^^ too.
and using plot_date(x,y,'bo-')
How do I make it so that the result of plot_date looks like the first picture? I have looked all over the Matplotlib website and couldn't find anything.
Thanks in advance
Upon further investigation I found that in order to display a solid line without dots, I needed to use the line style 'b-', making the code plot_date(x,y,'b-').
If you want to have the markers connected with the lines, you can also use
plt.plot_date(y,x,linestyle='solid')
So the complete code for the beginners would be:
from matplotlib import pyplot as plt
plt.plot_date(y,x,linestyle='solid')
plt.show()
Do not forget you need to have matplotlib installed first. For that you need to open command line and write:
pip install matplotlib

How to make the matplotlib subscript and special characters?

I want to make a subscript for the axis label of my plot plotted using matplotlib. I have the following script snapshot:
import matplotlib.pylab as plt
plt.ylabel(r'$A^{2}$')
I have 2 questions:
In the plot, the subscript "2" seems occupying a whole word length in stead of half, which make the plot a little bit weird. How to make the subscript smaller (both in size and the length span)?
How to display "angstroms" (the length unit of 10^-10m) instead of A?
Thanks!
What version of matplotlib are you using? In my version the superscript seems fine (to me).
Im using python 2.6.5 and matplotlib 1.1.0
Use \AA for angstram --> plt.ylabel("$\AA$").
The circle is a bit small, though.
If it's available to you, try using TeX to render your text.
from matplotlib import rc
rc('text', usetex=True)
#the rest of your plotting code here
This should make your fonts and text look a whole lot better.
For more details on matplotlib's TeX rendering capabilities, look here: http://matplotlib.sourceforge.net/users/usetex.html

Categories