Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ticks on Fixed Position , matplotlib

Can anyone help me set the ticks on a fixed position using matplotlib? I've tried using FixedPosition as this tutorial describes:

ax = pl.gca()
ax.xaxis.set_major_locator(eval(locator))

http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#figures-subplots-axes-and-ticks

But when I try to run, it tells me that set_major_locator method does not exist.

A simple example would be very useful.

Thanks.

like image 504
dantebarba Avatar asked Jun 16 '13 03:06

dantebarba


People also ask

How do I turn on minor ticks in MatPlotLib?

MatPlotLib with Python Plot x and y data points using plot() method. To locate minor ticks, use set_minor_locator() method. To show the minor ticks, use grid(which='minor').

How do I center a tick label in MatPlotLib?

However there is no direct way to center the labels between ticks. To fake this behavior, one can place a label on the minor ticks in between the major ticks, and hide the major tick labels and minor ticks.

How do I add a tick to a plot in Python?

Create x and y points using numpy. Plot x and y points over the plot, where x ticks could be from 1 to 10 (100 data points) on the curve. To add extra ticks, use xticks() method and increase the range of ticks to 1 to 20 from 1 to 10. To display the figure, use the show() method.


2 Answers

Just use ax.set_xticks(positions) or ax.set_yticks(positions).

For example:

import matplotlib.pyplot as plt  fig, ax = plt.subplots() ax.set_xticks([0.15, 0.68, 0.97]) ax.set_yticks([0.2, 0.55, 0.76]) plt.show() 

enter image description here

like image 62
Joe Kington Avatar answered Sep 24 '22 06:09

Joe Kington


import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt

name_list = ('Omar', 'Serguey', 'Max', 'Zhou', 'Abidin')
value_list = np.random.randint(0, 99, size = len(name_list))
pos_list = np.arange(len(name_list))

ax = plt.axes()
ax.xaxis.set_major_locator(ticker.FixedLocator((pos_list)))
ax.xaxis.set_major_formatter(ticker.FixedFormatter((name_list)))
plt.bar(pos_list, value_list, color = '.75', align = 'center')

plt.show()
like image 32
John Strong Avatar answered Sep 23 '22 06:09

John Strong