-: Hi guys, and welcome back.
In this video, we're going to adjust
the rotation of our x-axes ticks
so that they fit a bit better in our screen.
We're also going to explore the object-oriented approach
for doing this, because by default
we're always going to use the functional approach.
But we'll also see why it's often not recommended
to use the object-oriented approach
for setting this specific configuration.
So let's get to it.
Now that we've got enough room in our figure
for our rotated labels, all we have to do is do plt.x ticks
and then rotation equal 30.
The x ticks function can take a bunch more stuff,
including the labels themselves.
If we pass these in here, for example,
and we also pass in these,
then we can set the labels without having to pass them
to the bar chart just like that.
The x ticks function does need the index
or the coordinates of each column,
as well as the labels in order to do this.
So we can run app.py and you'll see that
now these are rotated.
However, I strongly recommend
that if you have a simple scenario
like the one we've got here,
you just call plt.x ticks with rotation equal 30
and put the labels in there.
Later on, we're go to learn
when you might want to put the labels
into this function directly.
But I just wanted to show you that it can be done.
All right, let's run this again.
Because as you can see, there is something wrong here.
And the rotation of the label has happened,
but the label is still very much centred in the tick.
So therefore, this is the middle of the label centre
in the tick, and it sort of almost spans
into the next column.
So we want to avoid that.
And how we can do that is by setting
the horizontal alignment.
And that is done with the ha parameter here.
So we do ha, right and that is gonna add
the horizontal alignment of the x ticks
to be right aligned.
And if we run this now, this looks a bit better.
Let's learn how to use the object-oriented approach
to do this instead of the functional approach.
But let me preceed that with a warning.
The object-oriented approach is valid,
but it is a bit more advanced
that changes some of the properties of the figure
that we haven't learned about.
And as such, it can produce some unwanted effects.
But I want to show you how to do it anyway,
just in case you come across it later on.
So the first thing that we would want to do
in the object-oriented approach is to do
axes.set x tick labels, and we want to pass in here,
the labels themselves,
as well as rotation equal 30 let's see.
So by doing this, we might be tempted to get rid
of the tick labels directly, so just get rid of them there.
But if we do this, we're no longer telling the axes
how many ticks there are, we're only telling the axes,
the names of the ticks that already exist.
So I'm going to run app.py and you'll see what I mean.
Here you can see that the first poll that we had
has in fact disappeared,
and we are left with an empty column label at the end.
The reason for that is because by default,
when matplotlib creates any sort of axes,
it adds the number of ticks needed
for displaying in the axes, which in this case,
it's six ticks.
As we can see, if we run this again
I'll show you there're are six different ticks,
and these are numbered from zero to five.
However, matplotlib also creates one extra tick underneath,
and one more tick at the top.
So really we have eight ticks in this graph,
but two of them are invisible.
They go from minus one
to zero, one, two, three, four, five and six,
which is also invisible.
When we assign the labels to the ticks,
we're assigning them starting at the first tick,
which is minus one, which is invisible.
So really there's a tick minus one here
that has the first label.
And then tick number zero has the second label
and tick number one has the third label and so on.
So it's a bit confusing, and it's clearly not what we want.
This is why I was saying the object-oriented approach
changes a couple of things
and that can make it a little bit more complicated.
So the next thing that we have to do
is say axes.set x ticks.
and here we can specify what ticks we want.
So we're gonna do range of length of polls
and that is going to set the ticks to zero to five,
instead of minus one to six.
So, I'm gonna run this again
and you'll see that now we get what we wanted.
The horizontal alignment is off,
but not a big deal at this point in time.
So the object-oriented approach gives you
a bit more flexibility,
but at the cost of needing to know a bit better
what's going on behind the scenes.
So normally, especially for simple plots,
I would recommend using plt.x ticks
instead of the object-oriented approach.
So I will go back to that.
Rotation equal 30 and ha equal right,
that's going to give us what we want, let's run this again.
You can see that we've got these numbers here
instead of the labels because we got rid of them
so we do have to bring them back.
And that is exactly what we wanted,
now we've got the labels there.
And with that, we're done.
Our chart now looks excellent and the labels are visible.
So thank you guys for joining me in this video.
I hope you've learned something
and I'll see you in the next one.