Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minor ticks in pandas plot

So I am trying to get minor tick grid lines to get displayed but they don't seem to appear on the plot. An example code is

data_temp = pd.read_csv(dir_readfile, dtype=float, delimiter='\t', 
                    names = names, usecols=[0,1,2,3,4])

result = data_temp.groupby(['A', 'D']).agg({'B':'mean', 'E':'mean'})
result2 = result.unstack()

x = np.arange(450, 700, 50, dtype = int)
plt.grid(True, which='both')
plt.minorticks_on()

result2.B.plot(lw=2,colormap='jet',marker='.',markersize=4, 
                  title='A v/s B', legend = True, grid = 'on' ,
                  xlim = [450, 700], ylim = [-70, -0], xticks = x)

What I get is

Plot

The major grid lines are displayed but the minor ones are not. I looked into the pandas documentation but just see the grid option. I was hoping to get the minor ticks grid lines to be a every 10th location on the X axis that is 460 470 etc and every location on the Y (actual scale of Y is a bit smaller)

like image 677
DBB Avatar asked Jul 13 '16 07:07

DBB


People also ask

How do you show minor ticks?

To show the minor ticks, use grid(which='minor'). To display the figure, use show() method.

How do I insert a minor tick in MatPlotLib?

Minor tick labels can be turned on by setting the minor formatter. MultipleLocator places ticks on multiples of some base. StrMethodFormatter uses a format string (e.g., '{x:d}' or '{x:1.2f}' or '{x:1.1f} cm' ) to format the tick labels (the variable in the format string must be 'x' ).

How do I get rid of minor ticks in MatPlotLib?

Matplotlib removes both labels and ticks by using xticks([]) and yticks([]) By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis.

What are major and minor ticks?

Ticks are always the same color and line style as the axis. Ticks come in two types: major and minor. Major ticks separate the axis into major units. On category axes, major ticks are the only ticks available (you cannot show minor ticks on a category axis).


1 Answers

Before plt.show() add plt.minorticks_on().

If you want to add minor ticks for selected axis then use:

ax = plt.gca()
ax.tick_params(axis='x',which='minor',bottom='off')
like image 130
Serenity Avatar answered Sep 21 '22 13:09

Serenity