How to Rotate Xticks on One Axis of Figure in Matplotlib Without "Getting" the Labels as a List

how to rotate xticks on one axis of figure in matplotlib without "getting" the labels as a list

i was able to use this approach. not sure if it is the most elegant way, but it works without outputting an array

for tick in ax[1].get_xticklabels():
tick.set_rotation(90)

Matplotlib: Rotate x-axis tick labels when don't know x-axis value variable

When you set x ticks labels with df_corr.columns you are using data's columns, which are 4 ('index', 'label_h1', 'label_h2', 'label_h3'), for only three boxes, so you should discard 'index' column:

ax.set_xticklabels([label for label in df.columns if label != 'index'],rotation = 90)

In alternative, as explained by Jody Klymak in the comment below, you can use a easier way with:

ax.tick_params(which='major', labelrotation=90)

Complete code

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv(r'data/data.csv')


fig, ax = plt.subplots(1,1, figsize=(13,10), dpi= 80)

sns.boxplot(data=df, ax = ax)

ax.tick_params(which='major', labelrotation=90)
ax.set_yscale("log")

plt.show()

Sample Image

How to rotate specific tick labels

After you set the tick positions (and optionally also set their labels), you can access the matplotlib text elements via ax.get_xticklabels().
So, you can just access the second element in the list and change its rotation. (Similarly, you can e.g. change colors.)

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(16, 8))
ax.set_xticks((0, 1455, 5000, 10000, 15000, 20000, 25000, 30000))
ticks = ax.get_xticklabels()
ticks[1].set_rotation(335)
plt.show()

rotating one tick label

This applies to pandas.DataFrame.plot, matplotlib, and seaborn.

rotating xticks causes the ticks partially hidden in matplotlib

One good option is to rotate the tick labels.

In your specific case, you might find it convenient to use figure.autofmt_xdate() (Which will rotate the x-axis labels among other things).

Alternatively, you could do plt.setp(plt.xticks()[1], rotation=30) (or various other ways of doing the same thing).

Also, as a several year later edit, with recent versions of matplotlib, you can call fig.tight_layout() to resize things to fit the labels inside the figure, as @elgehelge notes below.

Rotate existing axis tick labels in Matplotlib

When you're done plotting, you can just loop over each xticklabel:

for ax in [ax1,ax2,ax3]:
for label in ax.get_xticklabels():
label.set_rotation(90)

Matplotlib unable to rotate xticks

graph.plot.bar has a parameter "rot" for this purpose, see:
https://pandas.pydata.org/pandas-docs/version/0.15.2/generated/pandas.DataFrame.plot.html

import pandas as pd
import matplotlib as plt

Counts = {"Hate":hate, "Non-hate":noHate}
graph = pd.Series(Counts)

plt.rcParams['figure.figsize'] = (6,6)
plt.rcParams['font.size'] = (14)

graph.plot.bar(yticks=[hate,noHate],color=['tab:red', 'tab:green'], rot=20)

How to rotate x-axis tick labels in a pandas plot

Pass param rot=0 to rotate the xticklabels:

import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75, rot=0)
plt.xlabel("")
plt.show()

yields plot:

Sample Image



Related Topics



Leave a reply



Submit