Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move labels from bottom to top without adding "ticks"

How can I position xlabel on top of the plot using something else than "tick_top" - that adds a tick to the x label, and I don't want it.

xlabel on the bottom with no tick: enter image description here

code:

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.yticks(rotation=0)
plt.show()

xlabel on top but with tick: enter image description here

code:

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.yticks(rotation=0)
ax.xaxis.tick_top() # x axis on top
ax.xaxis.set_label_position('top')
plt.show()
like image 608
Kalanit Avatar asked Aug 09 '18 06:08

Kalanit


People also ask

How do you hide y ticks?

By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt. xticks([]) method to invisible both the ticks and labels on the x-axis and set the ticks empty.

How do you change a tick label in Python?

To fix the position of ticks, use set_xticks() function. To set string labels at x-axis tick labels, use set_xticklabels() function.

What is Labelpad?

labelpad is a distance in points from the axes bounding box including ticks and tick labels.


2 Answers

Try this:

plt.tick_params(axis='both', which='major', labelsize=10, labelbottom = False, bottom=False, top = False, labeltop=True)

the parameter top=False means no ticks. :)

like image 188
frozhen Avatar answered Oct 23 '22 12:10

frozhen


Ok finally found.

Needs a

ax.tick_params(length=0)
like image 6
Kalanit Avatar answered Oct 23 '22 12:10

Kalanit