loglog plots with inverted x_axis in python - python

I want to make a figure which has similar axis to the example below. I know I could use loglog plot. But in this example, the step-size (x-axis) decreases when you go farther to the right.
How could I do this in python (using matplotlib)

Possibly the invert_xaxis call is what you are looking for. As follows:
fig = plt.figure()
ax = fig.add_axes()
ax.invert_xaxis()
Link: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes

Related

How to adjust subplots borders in matplotlib automatically?

When plotting multiple plots using plt.subplots, most of the time the spacing between subplots is not ideal so the the xtick labels of the top plot would overlap with the title of the bottom plots. There is a way to fix this manually by calling say plt.subplots_adjust(hspace=0.5) and changing the parameters interactively to obtain a decent looking plot. Is there a way to calculate the subplot_adjust parameter automatically? Meaning finding the minimum hspace and wspace so that there is not overlap between texts of the plots.
You can use tight_layout https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html or constrained_layout https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html
I'm pretty certain that the closest your going to find to an inbuilt calculation method is:
plt.tight_layout()
or
figure.Figure.tight_layout() #if you are using the object version of the code

How do I draw borders around a matplotlib plot using object-oriented code?

I would like generate a plot with the coordinate axes in the middle of the plot area. Using matplotlib, I've managed to get as far as is shown in this sample code:
import matplotlib.pyplot as plt
xvalues = [-3,-2,-1,1,2,3]
yvalues = [2,4,-2,-4,1,-1]
fig, ax = plt.subplots()
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.scatter(xvalues, yvalues)
The problem with using set_position() to move the spines into the middle of the plot area is that this removes them as elements of the plot's border. I'm looking for a way to restore the border lines using object-oriented operations on the Figure and Axes instances fig and ax, respectively.
Please note that I'm interested in manifestly object-oriented code only: operations on fig and ax. This constraint is a part of the question.
I won't accept an answer given in terms of plt or equivalent. I already know how to do that. I'll accept an answer demonstrating that it isn't possible to draw these border lines using only manifestly object-oriented code before I accept an answer using plt.

Python - Smooth scatter plot

I have followed the post here in order to smooth a 3D scatter plot I have.
My original scatter plot is,
And I would like to get a smooth plot like the following one, that was made using Mathematica,
In the post I mentioned, they use the trisurf function to get a smoother plot. So I though I could use the same to get a similar plot. However, what I get is
As you can see, the triangulation did not work properly. And I don't know how to fix it.
Does anybody know a way to fix this problem? Or is there any other function I could use to smooth my scatter plot?
I think I should mention that my scatter plot is NOT a surface, it is a volume.
Thank you.
Just to clarify this, I post my codes for the original and the trisurf plot eventhough there isn't much to see.
Scatter plot:
S=pd.read_csv("SeparableStatesGrafica.csv",header=None,names=
['P0','P1','P2','P3','P4'])
G=plt.figure().gca(projection='3d')
G.scatter(S['P1'], S['P3'], S['P0'],color='red')
G.set_xlabel("P1")
G.set_ylabel("P3")
G.set_zlabel("P0")
G.view_init(40,40)
plt.show()
Trisurf plot:
S=pd.read_csv("SeparableStatesGrafica.csv",header=None,names=
['P0','P1','P2','P3','P4'])
p0=S['P0'].values
p1=S['P1'].values
p3=S['P3'].values
fig = pylab.figure(figsize=pyplot.figaspect(.96))
ax = Axes3D(fig)
ax.plot_trisurf(p1, p3, p0)
ax.set_xlabel("p1")
ax.set_ylabel("p3")
ax.set_zlabel("p0")
ax.view_init(40,40)
plt.show()

uneven axis when using pl.imshow

I am having trouble when plotting an image using pylabs imshow. Well there is no problem while plotting but my data is uneven (approx. 32*850) so when I plot it, the y axis is very short compared to the x-axis and you can see an example here example image. I just want the image to be stretched out in the y-axis so it is easier to see.
The code I started with(excluded labels and so on) is:
pl.figure()
pl.imshow(fom_data, interpolation='nearest')
pl.show()
And after googling it I tried changing to
pl.figure(figsize=(6,10))
Which only made the white parts around it larger. I then tried to write it with pyplot instead since it was easier to find people discussing the same thing:
fig, ax = plt.imshow(fom_data,extent=[0,850,0,32],aspect='auto')
plt.show()
As I found in this example: Imshow: extent and aspect but then get the following error message : 'AxesImage' object is not iterable
I am obiusly no pro, but if you know where my brain is going wrong please explain.
Using pyplot:
plt.figure()
plt.imshow(my_image)
plt.axes().set_aspect(aspect="auto") # grab the current axes to set their aspect

Empty python plot

I'm running a script remotely on a cluster to generate a scatter plot. I wish to save the plot, but I don't want the plot to be display or a window to come up (as when you execute plt.show() ).
My saved plots are always empty. This is the code that I'm using (below). Any tips would be very helpful. Thanks!
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([-1,maxX+1])
ax.set_ylim([0,maxY+1])
ax.set_xlabel('Comparison number (n)', fontsize=18, fontweight='bold')
ax.set_ylabel('Normalized cross correlation score', fontsize=18, fontweight='bold')
ax.scatter(xaxis,yaxis)
plt.savefig('testfig.png')
In order to use avoid showing plot windows (i.e. to do off-screen rendering) you probably want to use a different matplotlib backend.
Before any matplotlib import statements, add
import matplotlib
matplotlib.use('Agg')
and subsequent calls to matplotlib will not show any plot windows.
If your plot file shows an empty axis, then the problem lies in the plotting arguments as calling plot with empty arguments creates an empty axis.

Categories