xticks only applying to 1 graph - python

I'm trying to change the x axis for my graphs but it's only going through for one of them. My other graph has not changed at all. Here is the code:
fig, (gr0, gr1) = plt.subplots(ncols=2, constrained_layout=True, figsize = (17,7))
#gr0
gr0.plot(data['g1'])
gr0.set_title('text 1')
#gr1
gr1.plot(data['g2'])
gr1.set_title('text 2')
plt.xticks(fontsize=8, rotation=45)
plt.show()
Graphs when the code is ran:
As you can tell by the picture, only the graph on the right has the x-axis updated to where the text is rotated and clearly visible. The other one is still the same :(

The
plt.xticks(..., rotation=45)
call might feel like it applies to the whole figure,
when it's phrased that way.
But behind the scenes it's really making a gca() call
and manipulating that, which of course is gr1 at that point.
You could make a pair of xticks calls,
much as you're already doing for title.
But the fact that you asked about this plot's behavior on SO
indicates that the way it's phrased is not a good match
for clearly communicating the intent.
So let's re-phrase it slightly, avoiding that global.
labels = [
'2022-05-21',
'2022-05-25',
'2022-05-29',
]
gr0.set_xticks(labels=labels, rotation=45)
gr1.set_xticks(labels=labels, rotation=45)
Or consider moving to import seaborn.
In which case the idiom would be the somewhat simpler
gr0.grid.set_xticklabels(rotation=45)
gr1.grid.set_xticklabels(rotation=45)

You can set it per axis using ax.tick_params(labelsize=8, labelrotation=45).
ig, (gr0,gr1) = plt.subplots(ncols=2, constrained_layout=True, figsize = (17,7))
#gr0
gr0.plot(data['g1'])
gr0.set_title('text 1')
gr0.tick_params(labelsize=8, labelrotation=45)
#gr1
gr1.plot(data['g2'])
gr1.set_title('text 2')
gr1.tick_params(labelsize=8, labelrotation=45)
plt.show()

Related

Using pyplot from Plots.jl. How to make several subplots have only one colobar?

I am using Plots.jl to make several plots in the same figure. When using the pyplot backend, each plot has it's own colorbar, which I don't want as they have the same data. I am trying to replicate the answer from this question, however I don't know in detail of the machinery under the Plots.jl API, so I haven't been able to replicate it. My plot is done as:
using Plots;pyplot()
p1 = plot(a,st=:contour,fill=true)
p2 = plot(b,st=:contour,fill=true)
p = plot(p1,p2)
And, the answer (which is in python) from the link is this:
fig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
plt.show()
As far as I understand, the code inside the for is actually making the plots in the axes created by plt.subplots (in my case this is done by Plots.jl
The next line makes the plots closer, and then the line fig.add_axes creates a new axis for the colorbar.
Finally, the line of fig.colorbar creates a colorbar in the new axis and uses the data from the last plot in the for loop.
My current code is:
cbar_ax = p.o[:add_axes]([0.85, 0.15, 0.05, 0.7]);
p.o[:colorbar](p.o[:axes][1][:contourf],cax=cbar_ax)
display(p)
And it doesn't work (I wouldn't expect it to work because I don't know what I'm doing.
The error I get is:
AttributeError("'function' object has no attribute 'autoscale_None'")
Which makes me think p.o:axes[:contourf] is not the way to summon what I am trying to.
Can anyone help out? Thanks
In general, if you want to use code on the PyPlot object it's better to just use PyPlot and forget about Plots. The mix rarely works in practice.
If you do want to use Plots you should be able to do
using Plots;pyplot()
lims = extrema([a;b])
p1 = plot(a,st=:contour,fill=true, colorbar = false)
p2 = plot(b,st=:contour,fill=true, colorbar = true, clims = lims)
p = plot(p1,p2)
One of the subplots will be much wider than the other - you probably need to adjust with #layout to get them the same width.

Unable to remove legend box [python]

So I need to make this plot in python. I wish to remove my legend's border. However, when I tried the different solutions other posters made, they were unable to work with mine. Please help.
This doesn't work:
plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'})
plt.legend(frameon=False)
plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'})
plt.legend.get_frame().set_linewidth(0.0)
plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'}, 'Box', 'off')
Additionally, when I plotted, I imported two different files and graphed them with a line and with circles respectively. How could I put a line or a circle within the legend key?
The plot:
It's very strange because the command :
plt.legend(frameon=False)
Should work very well.
You can also try this command, to compare :
plt.legend(frameon=None)
You can also read the documentation on this page about plt.legend
I scripted something as example to you :
import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,4,8,13])
y = np.array([0,1,2,3])
fig1, ((ax1, ax2)) = plt.subplots(1, 2)
ax1.plot(x,y, label=u'test')
ax1.legend(loc='upper left', frameon=False)
ax2.plot(x,y, label=u'test2')
ax2.legend(loc='upper left', frameon=None)
plt.show()
Try this if you want to draw only one plot (without subplot)
plt.legend({'z$\sim$0.35', 'z$\sim$0.1','z$\sim$1.55'}, frameon=False)
It is enough one plt.legend. The second one rewrites the first one.
Make sure frameon = False is together with the positional argument in plt.legend(...) if you want to specify the position as well as remove the border. If these arguments are written separately or in sequential, there's an issue of overwriting and the desired effect may not be achieved.
Correct!
plt.legend(loc="lower right", frameon=False)
May not give desired effect when written like this!
plt.legend(loc="lower right") & plt.legend(frameon=False)

Matplotlib: set axis tight only to x or y axis

I have a plot look like this:
Obviously, the left and right side is a waste of space, so I set
plt.axis('tight')
But this gives me plot like this:
The xlim looks right now, but the ylim is too tight for the plot.
I'm wondering, if I can only set axis(tight) only to x axis in my case?
So the plot may look something like this:
It's certainly possible that I can do this manually by
plt.gca().set_xlim(left=-10, right=360)
But I'm afraid this is not a very elegant solution.
You want to use matplotlib's autoscale method from the matplotlib.axes.Axes class.
Using the functional API, you apply a tight x axis using
plt.autoscale(enable=True, axis='x', tight=True)
or if you are using the object oriented API you would use
ax = plt.gca() # only to illustrate what `ax` is
ax.autoscale(enable=True, axis='x', tight=True)
For completeness, the axis kwarg can take 'x', 'y', or 'both', where the default is 'both'.
I just put the following at the beginning of those scripts in which I know I'll want my xlims to hug my data:
import matplotlib.pyplot as plt
plt.rcParams['axes.xmargin'] = 0
If I decide to add some whitespace buffer to an individual plot in that same script, I do it manually with:
plt.xlim(lower_limit, upper_limit)
While the accepted answer works, and is what I used for a while, I switched to this strategy because I only have to remember it once per script.

Pyplot: Shared axes and no space between subplots

This is related to (or rather a follow-up) to new pythonic style for shared axes square subplots in matplotlib?.
I want to have subplots sharing one axis just like in the question linked above. However, I also want no space between the plots. This is the relevant part of my code:
f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
plt.setp(ax1, aspect=1.0, adjustable='box-forced')
plt.setp(ax2, aspect=1.0, adjustable='box-forced')
# Plot 1
ax1.matshow(pixels1, interpolation="bicubic", cmap="jet")
ax1.set_xlim((0,500))
ax1.set_ylim((0,500))
# Plot 2
ax2.matshow(pixels2, interpolation="bicubic", cmap="jet")
ax2.set_xlim((0,500))
ax2.set_ylim((0,500))
f.subplots_adjust(wspace=0)
And this is the result:
If i comment out the two plt.setp() commands, I get some added white borders:
How can I make the figure look like my first result, but with axes touching like in the second result?
EDIT: The fastest way to get your result is the one described by #Benjamin Bannier, simply use
fig.subplots_adjust(wspace=0)
The alternative is to make a figure that has a width/height ratio equal to 2 (as you have two plots). This may be advisable only if you plan including the figure in a document, and you already know the columnwidth of the final document.
You can set width and height in the call to Figure(figsize=(width,height)), or as a parameter to plt.subplots(), in inches. Example:
fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True,figsize=(8,4))
fig.subplots_adjust(0,0,1,1,0,0)
Screenshot:
As #Benjamin Bannier points out, as a drawback you have zero margins. Then you can play with subplot_adjust(), but you must be careful with making space in a symmetric way if you want to keep the solution simple. An example could be fig.subplots_adjust(.1,.1,.9,.9,0,0).

Rotate axis text in python matplotlib

I can't figure out how to rotate the text on the X Axis. Its a time stamp, so as the number of samples increase, they get closer and closer until they overlap. I'd like to rotate the text 90 degrees so as the samples get closer together, they aren't overlapping.
Below is what I have, it works fine with the exception that I can't figure out how to rotate the X axis text.
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import datetime
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 8}
matplotlib.rc('font', **font)
values = open('stats.csv', 'r').readlines()
time = [datetime.datetime.fromtimestamp(float(i.split(',')[0].strip())) for i in values[1:]]
delay = [float(i.split(',')[1].strip()) for i in values[1:]]
plt.plot(time, delay)
plt.grid(b='on')
plt.savefig('test.png')
This works for me:
plt.xticks(rotation=90)
Many "correct" answers here but I'll add one more since I think some details are left out of several. 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).
Easiest / Least Code
Option 1
plt.xticks(rotation=45, ha='right')
As mentioned previously, that may not be desirable if you'd rather take the Object Oriented approach.
Option 2
Another 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)
fig you would usually get from:
fig = plt.gcf()
fig = plt.figure()
fig, ax = plt.subplots()
fig = ax.figure
Object-Oriented / Dealing directly with ax
Option 3a
If you have the list of labels:
labels = ['One', 'Two', 'Three']
ax.set_xticks([1, 2, 3])
ax.set_xticklabels(labels, rotation=45, ha='right')
In later versions of Matplotlib (3.5+), you can just use set_xticks alone:
ax.set_xticks([1, 2, 3], labels, rotation=45, ha='right')
Option 3b
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,
# otherwise get_xticklabels() will return empty strings.
plt.draw()
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
As 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')
Option 4
Similar to above, but loop through manually instead.
for label in ax.get_xticklabels():
label.set_rotation(45)
label.set_ha('right')
Option 5
We still use pyplot (as plt) here but it's object-oriented because we're changing the property of a specific ax object.
plt.setp(ax.get_xticklabels(), rotation=45, ha='right')
Option 6
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.
ax.tick_params(axis='x', labelrotation=45)
Edit:
There's discussion of this exact "bug" but a fix hasn't been released (as of 3.4.0):
https://github.com/matplotlib/matplotlib/issues/13774
Easy way
As described here, there is an existing method in the matplotlib.pyplot figure class that automatically rotates dates appropriately for you figure.
You can call it after you plot your data (i.e.ax.plot(dates,ydata) :
fig.autofmt_xdate()
If you need to format the labels further, checkout the above link.
Non-datetime objects
As per languitar's comment, the method I suggested for non-datetime xticks would not update correctly when zooming, etc. If it's not a datetime object used as your x-axis data, you should follow Tommy's answer:
for tick in ax.get_xticklabels():
tick.set_rotation(45)
Try pyplot.setp. I think you could do something like this:
x = range(len(time))
plt.xticks(x, time)
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.plot(x, delay)
Appart from
plt.xticks(rotation=90)
this is also possible:
plt.xticks(rotation='vertical')
I came up with a similar example. Again, the rotation keyword is.. well, it's key.
from pylab import *
fig = figure()
ax = fig.add_subplot(111)
ax.bar( [0,1,2], [1,3,5] )
ax.set_xticks( [ 0.5, 1.5, 2.5 ] )
ax.set_xticklabels( ['tom','dick','harry'], rotation=45 ) ;
If you want to apply rotation on the axes object, the easiest way is using tick_params. For example.
ax.tick_params(axis='x', labelrotation=90)
Matplotlib documentation reference here.
This is useful when you have an array of axes as returned by plt.subplots, and it is more convenient than using set_xticks because in that case you need to also set the tick labels, and also more convenient that those that iterate over the ticks (for obvious reasons)
If using plt:
plt.xticks(rotation=90)
In case of using pandas or seaborn to plot, assuming ax as axes for the plot:
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
Another way of doing the above:
for tick in ax.get_xticklabels():
tick.set_rotation(45)
My answer is inspired by cjohnson318's answer, but I didn't want to supply a hardcoded list of labels; I wanted to rotate the existing labels:
for tick in ax.get_xticklabels():
tick.set_rotation(45)
The simplest solution is to use:
plt.xticks(rotation=XX)
but also
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=X.XX)
e.g for dates I used rotation=45 and bottom=0.20 but you can do some test for your data
import pylab as pl
pl.xticks(rotation = 90)
To rotate the x-axis label to 90 degrees
for tick in ax.get_xticklabels():
tick.set_rotation(45)
It will depend on what are you plotting.
import matplotlib.pyplot as plt
x=['long_text_for_a_label_a',
'long_text_for_a_label_b',
'long_text_for_a_label_c']
y=[1,2,3]
myplot = plt.plot(x,y)
for item in myplot.axes.get_xticklabels():
item.set_rotation(90)
For pandas and seaborn that give you an Axes object:
df = pd.DataFrame(x,y)
#pandas
myplot = df.plot.bar()
#seaborn
myplotsns =sns.barplot(y='0', x=df.index, data=df)
# you can get xticklabels without .axes cause the object are already a
# isntance of it
for item in myplot.get_xticklabels():
item.set_rotation(90)
If you need to rotate labels you may need change the font size too, you can use font_scale=1.0 to do that.

Categories

Resources