Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show tick labels on top of matplotlib plot?

In matplotlib what is the way to have tick labels both at the bottom and in the top x axis? I have searched a lot and still can't find how to do it.

like image 583
elyase Avatar asked Sep 15 '13 02:09

elyase


People also ask

How do I show all tick labels in MatPlotLib?

Steps. Create a list of numbers (x) that can be used to tick the axes. Get the axis using subplot() that helps to add a subplot to the current figure. Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).

How do I turn on ticks in MatPlotLib?

To remove the ticks on both the x-axis and y-axis simultaneously, we can pass both left and right attributes simultaneously setting its value to False and pass it as a parameter inside the tick_params() function. It removes the ticks on both x-axis and the y-axis simultaneously.

How do I change the format of a tick in MatPlotLib?

Tick formatters can be set in one of two ways, either by passing a str or function to set_major_formatter or set_minor_formatter , or by creating an instance of one of the various Formatter classes and providing that to set_major_formatter or set_minor_formatter .


2 Answers

Sorry, I lied in the comments. You can do this easily (but it seems to be badly documented)

fig, ax = plt.subplots(1, 1)
ax.xaxis.set_tick_params(labeltop='on')

output

like image 76
tacaswell Avatar answered Oct 14 '22 16:10

tacaswell


You can do it with twiny():

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X2tick_location= ax1.xaxis.get_ticklocs() #Get the tick locations in data coordinates as a numpy array
ax2.set_xticks(X2tick_location)
ax2.set_xticklabels(X2tick_location)
plt.show()

enter image description here

Have a look to this question too for more elaborate plots.

like image 3
G M Avatar answered Oct 14 '22 14:10

G M