Overlaying subplot on seaborn factorplot - python

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.

Related

Change legend number range in Altair plot

I am trying to plot time series data in a kind of "climate stripes plot" using the package Altair.
The problem is that I do not know how to change the range in the legend to standardise all my plots with the same colour range and numbers in the legend. At the moment, each time I plot something the legend adapts to the range of the data.
I think the problem is with the "domain" property, maybe is not in the correct place ?
Thank you for your help :)
This is the code for the plot :
chart=alt.Chart(source).mark_rect().encode(
x=('day:O'),
y='subasins:N',
color=alt.Color('90%:Q',legend=alt.Legend(title='CH4'), bin=alt.Bin(maxbins=20),
scale=alt.Scale(scheme='blueorange'),domain=[1830,2000])
).properties(width=100).facet(column=alt.Column('month'))
chart.show()
Plots that I get now with different scales in the legend
You're using the right approach with domain, it just needs to be put inside alt.Scale:
scale=alt.Scale(scheme='blueorange', domain=[1830, 2000])
When you're using a bin transform, one way to ensure the scale is consistent is to specify the bin extent:
bin=alt.Bin(maxbins=20, extent=[1830, 2000])

Position a legend in a plot at a specified location, and only fallback to "best" if it is overlapping with smth (matplotlib)

I know that you can use ax.legend(loc='best') to tell matplotlib to find the location with the least overlap to place the legend in my plot. However, in my plots there are often several locations that have no overlap, but by using the "best" option the legends are put in different locations on different plots (I assume the ones most "far away" from any data points). I don't like this unnecessary inconsistency between the plots. I would like to have my legends always at ax.legend(loc='lower right'), and only fall back to ax.legend(loc='best') if the legend is actually overlapping with data points ... is there a a way this can be done generically?

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.

Show only a specified sub-region in a matplotlib contour plot

Am using matplotlib's contourf to do some contour plotting. I would like the contour plot to be filled with a particular sub-region of the data I want to display, leaving the rest of the data out of the picture. Is there a way to do this with contourf?
The xlim/ylim keywords do not help. No error returned, but no effect on the contour plot. Same with the "extent" keyword. Giving contourf just a subsection to plot is not an option (not an easy option, I should say) because my data is on an irregular grid.
If anyone's interested, it turns out it's pretty easy. I do the contour and then I set x and y limits with matplotlib.pyplot.xlim and ylim:
contourf(x,y,z)
xlim((1000,2000))
ylim((4000,7000))

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

Categories

Resources