2

I have to create a plot on which ticks and labels are specifically defined; an example of reproducible plot is given below:

import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn-v0_8')

fig, ax = plt.subplots(figsize=(4, 4))

ticks = [0.00, 0.25, 0.50, 0.75, 1.00]

ax.set_xticks(ticks)
ax.set_xticklabels(ticks, weight='bold', size=8)
ax.set_yticks(ticks)
ax.set_yticklabels(ticks, weight='bold', size=8)

plt.show()

As ticks and labels are exactly the same of both axes, is there a way to set them as a single command ? Something mixing both xticks and yticks ?

2 Answers 2

1

You can use a function.

import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8')

def set_ticks_and_labels(ax, t, **kwargs):
    ax.set_xticks(t)
    ax.set_xticklabels(t, **kwargs)
    ax.set_yticks(t)
    ax.set_yticklabels(t, **kwargs)

fig, ax = plt.subplots(figsize=(4, 4))

ticks = [0.00, 0.25, 0.50, 0.75, 1.00]
set_ticks_and_labels(ax, ticks, weight='bold', size=8)

plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

you can replace

ax.set_ticks(ticks)
ax.set_xticklabels(ticks, weight='bold', size=8)
ax.set_yticks(ticks)
ax.set_yticklabels(ticks, weight='bold', size=8)

by

for a in [ax.xaxis, ax.yaxis]:
    a.set_ticks(ticks)
    a.set_ticklabels(ticks, weight='bold', size=8)

but not sure that really answers your request


Notice set_xticklabels and set_yticklabels are discouraged, see set_xticklabels for instance, and this is the same for set_ticklabels

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.