How to matplotlib draw figure with different spacing - python

How to matplotlib draw figure with different spacing?
specifically, how to draw this one?

The plot shown in the question is plotted on a 'logit' scale.
ax.set_xscale('logit')
ax.set_yscale('logit')
You may refer to the scales example.

Related

How to add a clear border around a graph with matplotlib.pyplot

I created a stacked barchart using matplotlib.pyplot but there is no border around the graph so the title of the graph and axes are right up against the edge of the image and get cutoff in some contexts when I use it. I would like to add a small clear or white border around the graph, axes and title. repos_amount is a pandas DataFrame.
Here is my code:
colors = ["Green", "Red","Blue"]
repos_amount[['Agency','MBS','Treasury']].plot.bar(stacked=True, color=colors, figsize=(15,7))
plt.title('Total Outstanding Fed Repos Operations', fontsize=16)
plt.ylabel('$ Billions', fontsize=12)
Here is what the graph looks like:
I tried the suggestions from the link below and I could not figure out how to make it work. I'm not good with matplotlib yet so I would need help figuring out how to apply it to my code.
How to draw a frame on a matplotlib figure
Try adding plt.tight_layout() to the bottom of your code.
Documentation indicates that this tries to fit the titles, labels etc within the subplot figure size, rather than adding items around this figure size.
It can have undesirable results if your labels or headings are too big, in which case you would then need to look into the answers in this thread to adjust the specific box size of your chart elements.

Overlaying subplot on seaborn factorplot

I'm creating a plot with factorplot and then trying to add a subplot on top of each box. How can I get the x-axis locations of each individual box in the factor plot to put another line on top?
Maybe there's a way to get all the x-axis values of each box plot on the axes?
Here's my basic factor plot:
I want to add 1 subplot (the circle) in the middle of each box plot. However, I cannot figure out how to get the x-value of each box to properly space the points.
I see a lot of code for positions and offsets in the seaborn source that lays these out. However, I'm wondering if there is a more straight-forward method to get this information or at least approximate it.
As per #mwaskom's comments, you can use sns.stripplot() (and now also sns.swarmplot()) to include your data points with a data summary plot such as a box or violinplot.

Matplotlib offset errorbar bug workaround?

The bug is documented here:
Matplotlib errorbar not centered on marker
and here:
https://github.com/matplotlib/matplotlib/issues/3400
Basically, the markers are plotted off by 1 pixel all the time.. You can even see this on Matplotlib's own tutorial page if you look closely at the second plot: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html
This is very frustrating as I cannot produce publication-quality plots from matplotlib, and I'm very surprised this has not been fixed.
In any case, I have too much time and code invested into matplotlib to switch to a different package. So my question is how would you go about making a workaround? I suppose one solution is to plot the markers 1 pixel to the left/right from the errorbars. I don't know how to do this. I figured out how to get the display coordinates of my plot points, but how can I make an interactive plot that preserves the 1-pixel offset? I can plot them with 1-pixel offsets, but then you can't zoom or manipulate the plot.
It seems like the Matplotlib team have fixed the issue when calling savefig() using .svg or .pdf, but for .png I've found that you can circumvent this issue by using an odd number for the error line thickness. Using the first example on the Matplotlib tutorial, if we use
plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15)
then the bars are offset like this:
However if we use a line width of 3
plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15, elinewidth=3)
then the bars are centred like this:
This isn't a perfect solution, but it does the job if you don't mind having slightly thicker lines.

How can I draw inline line labels in matplotlib?

I have the following graph, consisting of several lines:
Now, I would like to label all the lines in the plot. However, using legend() crams all the labels together in a box, which makes the plot somewhat difficult to interpret. What I'd like to to instead is to use inline labels. My ideal output would use labels like the following matplotlib contour plot, but with text labels for lines instead of numbers:
I haven't been able to find out how to do this in the matplotlib documentation. Is there a way to achieve this? If not, what other software could I use to generate this type of plot?
May I suggest another solution to your problem. Since in your case legend overlaps the charts you might just want to move the legend outside of the plot.
Method do move legend outside of plot is described here:
Moving matplotlib legend outside of the axis makes it cutoff by the figure box

How to place country boarders as background for a plot with matplotlib?

I have some data made of coordinates and the count of each coordinate which I plot in a heatmap like this:
pyplot.subplot(211)
pyplot.scatter(longitudes, latitudes, c=counts)
pyplot.colorbar()
which is inspired by this great answer here in SO.
If you look closely you can see, that the dots shape the worldmap somehow. To underline this effect I'd like to put the real country boarders (simply drawn would be enough) as background to my plot. Is this possible with matplotlib? Maybe there is some (hidden) builtin in matplotlib?
You can likely achieve this if you have some image of the world map that you want as a background. You can read this into a numpy array and plot the image. Then you should be able to add your scatter plot overtop of the image. This matplotlib cookbook example shows how to insert images and such. There is also the matplotlib image tutorial that may be of use.
I've not used it, but you may also be interested in the basemap toolkit for matplotlib. In particular, the section on drawing a map background mentions specifically a drawcountries() method.

Categories

Resources