I would like to remove the numerical values in each thick of my y-axis (10, 20, 30, etc.) but keeping the thick marks and the axis label.
So far I just have:
yticks([])
but it removes the ticks as well. I also tried:
frame=gca()
frame.axes.get_yaxis().set_visible(False)
but it removes both axis label and axis ticks.
I agree with Mathieu that tick_params() is a better method (because you don't need to know the number of ticks in advance), however the most generalized parameter is "label1On," i.e.:
gca().tick_params(axis='x',label1On=False)
This approach easily extends to 'y' axis, as well as the second 'x' or 'y' axes (using label2On).
The tick_params() function should do the job:
gca().tick_params(axis='x',labelbottom='off')
You can set the tick labels to an empty list:
from matplotlib.pyplot import *
gca().set_xticklabels(['']*10)
plot(range(10))
Results in
I had to do that before I called plot. Not sure why the other way around didn't work
Related
I plotted a dataframe using the following command
df['col1'].value_counts().plot.bar(orientation='vertical')
The output is shown as above. I would like to change two things:
swap the x and y axis so i can read the current x axis text from left to right (NOTE the current x axis labels contains long text and it is hard to read it vertically. Picture is truncated at the bottom for simplicity of this post)
add line spacing between the labels for the current x axis so it helps reading (will be on the y axis if step 1 is done). If increasing the size of the plot is needed, please suggest how to do so.
I have tried a few things but none of them have any visible effects. e.g.,
plt.xticks(linespacing=1.5)
np.linspace(-1, 1, 10000)
You can plot horizontally directly. Also, consider setting a different font size in advance, e.g.:
plt.rcParams.update({'font.size': 6}) # Arbitrary
df['col1'].value_counts().plot(kind="barh", figsize=(5, 20))
I´m looking to add a specific range of values to the x-axis of my plot and increase the length of this axis.
I change the range of the values of my x-axis; however, the values keep in a specific range.
Besides, I tried to increase the length of the x-axis but I failed again.
For now, I´m only plotting an empty graph, because a need to set the specifications for the axis.
Here is part of the code to the plot:
fig1, ax = plt.subplots()
ax.set_xlim(1, 1200)
ax.set_ylim(-800, 200)
ax.set_box_aspect(1)
plt.show()
This code gives me a plot square with the range of the:
x-axis = 0-200-400...1200,
I´m looking for:
x-axis = 0-50-100-150...1200
Also, I need to change the shape of the plot: square to a rectangular, where the x-axis increases the length.
Any suggestion or comment is welcome!
Thank!
plt.figure(figsize=(15,2))
Use this at first line to set the size of your plot. As you want to increase x-axis, then see that x>y in figsize parameter.
l1=np.arange(0,1250,50)
plt.xticks(l1)
Use the above code after setting y limits to set the xticks in range of 0-1200 with gap of 50.
``
You can change the size (and therefore the shape) of a pyplot figure like this:
fig1.set_size_inches(10, 8)
As for the ticks on the axis, this post gives a pretty in-depth answer on how to customize those.
In this plot, matplotlib automatically hides the first and last axis labels. However, the desired behavior is to always show both the first and last axis labels and that too as a 'nice' value. By 'nice' value, I mean a major tic should be present at both boundaries of the axis. For example, in the figure shown below, the x-axis would have started from -0.1 and ended at 1.5. Similarly, the y-axis would have started from -0.25 and ended at 2.00. How can this be achieved in Matplotlib?
Thanks in advance for your help.
I solved this problem by first letting matplotlib find the ideal tick locations and then setting the axis limits such that one additional tick is added on both the edges of the axis.
plt.figure()
plt.plot(xdata, data)
loc, labels = plt.xticks() #returns the current tics and lables.
min_new = loc[0] - (loc[1]-loc[0])
max_new = loc[len(loc)-1] + (loc[1]-loc[0])
plt.xlim(left=min_new, right=max_new)
plt.gca().xaxis.set_major_locator(AutoLocator())
plt.gca().yaxis.set_major_locator(AutoLocator())
plt.show()
Edit:
The same could also be achieved by extending axis limits to the hidden ticks at the edges. i.e.,
loc, labels = plt.xticks()
plt.xlim(left=loc[0], right=loc[len(loc)-1])
I'm trying to create a plot which has y axis exactly same with this :
And I'm in this situation and everything is ok until this point:
When i try this lines:
ax.set_yticks(range(20,67,10))
ax.set_yticklabels(['20','30','40','50','60'])
My graph is becoming this:
I couldn't understand how to set locations' of yticks properly.
Have you tried the following?
import numpy as np
ticks = np.linspace(20, 60, 5)
ax.set_yticks(ticks)
ax.set_yticklabels([str(int(x)) for x in ticks])
If you want numbers on the axes, make sure you plot numbers, not strings. The axis labels would then adjust automatically as desired, or you may use set_yticks and set_yticklabels as in the question.
I have a simple plot code as
plt.plot(x,y)
plt.show()
I want to add some extra ticks on the x-axis in addition to the current ones, let's say at
extraticks=[2.1, 3, 7.6]
As you see I do not have a pattern for ticks so I do not want to increase the tick frequency for the whole axis; just keep the original ones and add those extras...
Is it possible, at all?
Regards
Yes, you can try something like:
plt.xticks(list(plt.xticks()[0]) + extraticks)
The function to use is xticks(). When called without arguments, it returns the current ticks. Calling it with arguments, you can set the tick positions and, optionally, labels.
For the sake of completeness, I would like to give the OO version of #Lev-Levitsky's great answer:
lines = plt.plot(x,y)
ax = lines[0].axes
ax.set_xticks(list(ax.get_xticks()) + extraticks)
Here we use the Axes object extracted from the Lines2D sequence returned by plot. Normally if you are using the OO interface you would already have a reference to the Axes up front and you would call plot on that instead of on pyplot.
Corner Caveat
If for some reason you have modified your axis limits (e.g, by programatically zooming in to a portion of the data), you will need to restore them after this operation:
lim = ax.get_xlim()
ax.set_xticks(list(ax.get_xticks()) + extraticks)
ax.set_xlim(lim)
Otherwise, the plot will make the x-axis show all the available ticks on the axis.