There's discussion of this exact "bug" but a fix hasn't been released (as of 3.4. ax.tick_params(axis='x', labelrotation=45) This option is simple, but AFAIK you can't set label horizontal align this way so another option might be better if your angle is not 90. matplotlib subplot Share Improve this question Follow edited Jun 8 at 17:16 Trenton McKinney 55.3k 33 138 150 asked at 16:47 Zengjie Song 15 5 Add a comment 1 Answer Sorted by: 3 A minimal working answer for you. plt.setp(ax.get_xticklabels(), rotation=45, ha='right') We still use pyplot (as plt) here but it's object-oriented because we're changing the property of a specific ax object. Similar to above, but loop through manually instead. ![]() # otherwise get_xticklabels() will return empty strings.Īx.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')Īs above, in later versions of Matplotlib (3.5+), you can just use set_xticks alone: ax.set_xticks(ax.get_xticks(), ax.get_xticklabels(), rotation=45, ha='right') If you want to get the list of labels from the current plot: # Unfortunately you need to draw your figure first to assign the labels, In later versions of Matplotlib (3.5+), you can just use set_xticks alone: ax.set_xticks(, labels, rotation=45, ha='right') ![]() If you have the list of labels: labels = Īx.set_xticklabels(labels, rotation=45, ha='right') Object-Oriented / Dealing directly with ax Option 3a Option 2Īnother fast way (it's intended for date objects but seems to work on any label doubt this is recommended though): fig.autofmt_xdate(rotation=45) Easiest / Least Code Option 1 plt.xticks(rotation=45, ha='right')Īs mentioned previously, that may not be desirable if you'd rather take the Object Oriented approach. The OP asked for 90 degree rotation but I'll change to 45 degrees because when you use an angle that isn't zero or 90, you should change the horizontal alignment as well otherwise your labels will be off-center and a bit misleading (and I'm guessing many people who come here want to rotate axes to something other than 90). All of the documentation.Many "correct" answers here but I'll add one more since I think some details are left out of several.And my answer on this too: How to plot the (x, y) text for each point using plt.text(), and handle the first and last points with custom text formatting.Matplotlib: Display value next to each point on chart.# subplots as you want, call this to show all figures! Finally, when done adding all of the figures you want to, each with as many Use `bottom=0.2` to bring the bottom of the plot up to leave space for the # the plot for the figure subtitle to go above the plot title! Use `top=0.8` to bring the top of the plot down to leave some space above 18.5k 4 53 64 asked at 0:40 Eagle 3,354 5 34 46 Add a comment 5 Answers Sorted by: 110 I get the correct alignment when I format the string this way: import matplotlib.pylab as plt fig plt.figure ()num0,figsize (8.27, 11.69), dpi300) ax fig.addsubplot (2, 2, 1) ax.settitle ('Normalized occupied Neighbors') plt. # configure your figure title, subtitle, and footer. When all done adding as many subplots as you want to for your figure, ![]() Plt.plot(x_vals, y_vals, 'r-o', label="Drag curve for Vehicle 1") Summary of other plot features and titles: # Figure title (super-title)įig.suptitle("Figure title", fontsize=16)įig.text(0.5, 0.015, "Figure footer: see my website at horizontalalignment="center") We use horizontalalignment="center" to ensure it stays centered left and right. The 0.9 y-location puts it a little down from the top, so that it will end up under the Figure title. The 0.5 x-location is the halfway point between the left and the right. Here's how to do a subtitle: just use a regular figure text box stuck in the right place: # Figure subtitleįig.text(0.5, 0.9, "Figure subtitle", horizontalalignment="center") It's pretty thorough, for all your title and label needs. Here's a hello world I wrote as I was figuring out how to use matplotlib for my needs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |