I use the matplotlib boxplot and plotted this graph. My question is how can I make the box larger or make the y-axis interval wider?
To resize the plot or to enlarge the resulting image, you could tweak the figsize parameter in matplotlib.pyplot.figure.
To reduce the size of the markers to improve the visibility of the dots/circles, you could do that using the linewidth parameter.
You could share a snippet of your code for more accurate answers.
Related
I got a general issue with a plotting function using matplotlib.pyplot. I plot a set of stacked horizontal bar charts, the goal is to produce congruent figures, the embedded grid is suppose to fit, but the image gets resized, when I change the input data. However, if i plot the image without the legend, I receive the desired output. But I do want to include a legend of course !
The formatting lines I include are the following:
figure size
fig,ax = plt.subplots(1,figsize=(20,9))
grid definition
plt.grid(color="darkgrey")
legend defintion
ax.legend([handles[i] for i in order], [labels[i] for i in order],
bbox_to_anchor=((0.5, -0.15)), loc="upper center",fontsize="medium",ncol=5)
tight layout
fig.tight_layout()
store the image
fig.savefig(str(figname)+'.jpg',bbox_inches='tight',quality=95,dpi=300)
I am trying to make automated plots with matplotlib, with several different features plotted on top of one another (the background is a filled contour plot, one level above is a pcolormesh). The topmost feature that I'm trying to plot is several scatter plots, with different labels and icons.
I'm trying to add a legend to this plot, currently using the following commands:
leg = ax.legend(legplots,
legnames,
scatterpoints=1,
loc='upper center',
ncol=3,
fontsize=14,
bbox_to_anchor=(0.5, -0.14),
fancybox=True, shadow=True)
Assume that ax is the main axes, and that legplots and legnames are lists of scatter plots and their appropriate labels respectively.
Adding the legend works correctly, but as the number of legplots that I have (and their name lengths) vary, as you animate the plots, the legend grows and shrinks in size. How do I control the size of the legend box and the column widths inside the legend? Is this possible?
At the moment your bounding box has a size of 0, since you only specify its position ((0.5, -0.14)).
You can set the bounding box of the legend to be bigger than 0 and also big enough for the maximum size that it needs to have for the maximum number of elements to fit in. I think you will need to find that size by trial and error.
So using the full 4-tuple notation
bbox_to_anchor=(x0, y0, width, height)
in conjunction with an appropriate loc parameter and the keyword argument mode="expand" will allow you to make the legend big enough for your needs. For a more detailed explanation about the 4-tuple notation see this post and also look at the legend location guide.
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.
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.
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.