"Invert" Axis on Matplotlib / Seaborn - python

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!

Related

Name of this 'horizontal section' diagram and how to implement in PyPlot

I need to create a plot with matplotlib/pyplot in a very specific way. I want the curve to never go up again, instead I want these horizontal sections with the little overstanding line, like shown in the picture:
Source: https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html#sphx-glr-auto-examples-model-selection-plot-precision-recall-py
My problem is I don't know how this type of line-style is even called so I don't know what to search for. It is often used for PR-Curves in deep learning.
Does anyone know how this is called and maybe also how to plot a graph in this style with pyplot?
This is my current approach by just plotting with the plot function. As you can see there are these rising edges which I want to get rid of.
plt.plot(x, y, linewidth=3)
The example you cited from scikit-learn uses drawstyle='steps-post' (this demo illustrates what it does), so try
plt.plot(x, y, drawstyle='steps-post')

Python/Seaborn: What does the inside horizontal distribution of the data-points means or is it random?

It seems like that inside-distribution of the histogram data points is almost random every time you plot (using Seaborn) - is it for the ease of readability or other meaningful purpose?
I am using Python 3.0 and Seaborn provided dataset called 'tips' for this question.
import seaborn as sns
tips = sns.load_dataset("tips")
After I ran my same code below twice I see differences of inside points distribution. Here is the code you can run a couple of times:
ax = sns.stripplot(x="day", y="total_bill", data=tips, alpha=.55,
palette='Set1', jitter=True, linewidth=1 )
Now, if you look into the plots (if you ran it twice for example) you will notice that the distribution of the points is not the same between 2 plots:
Please explain why points are not distributed identically with 2 separate runs? Also, judging those points on the horizontal scale; is there a reason why (for example) one red point is further left than other red point OR is it simply for readability?
Thank you in advance!
After a bit more research, I believe that the distribution of data points is random but uniform (thank you #ImportanceOfBeingErnest for pointing to the code). Therefore, answering my own questions there is no hidden meaning in terms of distribution and horizontal range is simply set for visibility that also changes or stays the same based on set/notset seed.
I do think that both displays are identical along the vertical axis (I.e. : both distributions are equal since they represent the same scatter plot of a given dataset). The slight visual differences comes along the position onto the horizontal (categorical days) axis; this one comes from the 'jitter' option (=True) that induces slight random relatively to the vertical axis they are related to (day). The jitter option helps to distinguish scatter plots with the same total_bill value (that should be superimposed if equal) : thus the difference comes from the jitter option set to True, that is used for readability.

Re-adjusting (automatically) limits on plot in matplotlib

Is there a way to let matplotlib know to recompute the optimal bounds of a plot?
My problem is that, I am manually computing a bunch of boxplots, putting them at various locations in a plot. By the end, some boxplots extend beyond the plot frame. I could hard-code some xlim and ylim's for now, but I want a more general solution.
What I was thinking was a feature where you say "ok plt I am done plotting, now please adjust the bounds so that all my data is nicely within the bounds".
Is this possible?
EDIT:
The answer is yes.
Follow-up question: Can this be done for the ticks as well?
You want to use matplotlib's automatic axis scaling. You can do this with either axes.axis with the "auto" input or axes.set_autoscale_on
ax.axis('auto')
ax.set_autoscale_on()
If you want to auto-scale only the x or y axis, you can use set_autoscaley_on or set_autoscalex_on.

Matplotlib: Avoid congestion in X axis

I'm using this code to plot a cumulative frequency plot:
lot = ocum.plot(x='index', y='cdf', yticks=np.arange(0.0, 1.05, 0.1))
plot.set_xlabel("Data usage")`
plot.set_ylabel("CDF")
fig = plot.get_figure()
fig.savefig("overall.png")
How it appears as follows and is very crowded around the initial part. This is due to my data spread. How can I make it more clear? (uploading to postimg because I don't have enough reputation points)
http://postimg.org/image/ii5z4czld/
I hope that I understood what you want: give more space to the visualization of the "CDF" development for smaller "data usage" values, right? Typically, you would achieve this by changing your X axis scale from linear to logarithmic. Head over to Plot logarithmic axes with matplotlib in python for seeing different ways to achieve that. The simplest might be, in your case, to replace plot() with semilogx().

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

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.

Categories