I've made the following scatterplot. I have an outlier on the positive side of the graph and also the negative end of the graph.
I think normally if I were plotting this using matplotlib.axes.Axes.plot I could use clip_on
But since this was graphed using matplotlib.pyplot.scatter there is no clipping parameter. Do I basically have to adjust the data somehow? Or is there a matplotlib way of doing this?
Related
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
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.
In a standard 3D python plot, each data point is, by default, represented as a sphere in 3D. For the data I'm plotting, the z-axis is very sensitive, while the x and y axes are very general, so is there a way to make each point on the scatter plot spread out over the x and y direction as it normally would with, for example, s=500, but not spread at all along the z-axis? Ideally this would look like a set of stacked discs, rather than overlapping spheres.
Any ideas? I'm relatively new to python and I don't know if there's a way to make custom data points like this with a scatter plot.
I actually was able to do this using the matplotlib.patches library, creating a patch for every data point, and then making it whatever shape I wanted with the help of mpl_toolkits.mplot3d.art3d.
You might look for something called "jittering". Take a look at
Matplotlib: avoiding overlapping datapoints in a "scatter/dot/beeswarm" plot
It works by adding random noise to your data.
Another way might be to reduce the variance of the data on your z-axis (e.g. applying a log-function) or adjusting the scale. You could do that with ax.set_zscale("log"). It is documented here http://matplotlib.org/mpl_toolkits/mplot3d/api.html#mpl_toolkits.mplot3d.axes3d.Axes3D.set_zscale
Given below is the code for plotting points using pyplot.
x1=300+p[k]*math.cos(val[thetaval])
y1=300+p[k]*math.sin(val[thetaval])
plt.plot(x1,y1,'k.')
The plotting is working fine, the problem is, if I want to plot it as a point I am specifying the dot in 'k.' inside the plot function. The output is something like:
The width of the black line/curve that I am plotting is much more that needed. How to reduce it?
It seems that you are not plotting a line but a series of small points. Maybe if you try setting the markersize argument of the plot function could work.
Looking into the documentation of plot() you can find "linewidth"
So use:
plt.plot(x1,y1,'k.', linewidth=0.1)
I have a collection of measured tree diameters and am trying to plot a histogram with a kernel density estimation superimposed on top in python. The seaborn module lets me do this quite simply but I can find no way of specifying that the kde should be zero for negative numbers (since trees can't have negative tree diameters).
what I've got at present is this:
seaborn.distplot(C77_diam, rug=True, hist=True, kde=True)
I've looked at seaborn.kdeplot which is the function that distplot calls but can't find anything useful. Does anyone know if this can be done with seaborn, and if not if it can be done with matplotlib more generally?
I only started using seaborn because i couldn't figure out how to overlay a kde pyplot.plot() with a pyplot.hist().
There's no way to force the density estimate to zero with that function, but you can always set the axis limits such that the left side of the plot starts at 0.
seaborn.distplot(C77_diam, rug=True, hist=True, kde=True).set(xlim=(0, max_diam))