Home > Mobile >  matplotlib ax make x labels smaller
matplotlib ax make x labels smaller

Time:04-25

My plot now looks like: enter image description here

I would like to read the x-labels better. my code is:

# Plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4), dpi=120, sharey=True)

ax1.bar(x='Dominant_Topic', height='count', data=df_dominant_topic_in_each_doc, width=.5, color='firebrick')

ax1.set_xticks(range(df_dominant_topic_in_each_doc.Dominant_Topic.unique().__len__()))
tick_formatter = FuncFormatter(lambda x, pos: 'Topic '   str(x)  '\n'   df_top3words.loc[df_top3words.topic_id==x, 'words'].values[0])
ax1.xaxis.set_major_formatter(tick_formatter)

ax1.set_title('Number of Documents by Dominant Topic', fontdict=dict(size=10))
ax1.set_ylabel('Number of Documents')
ax1.set_ylim(0, 4000)

ax2.bar(x='index', height='count', data=df_topic_weightage_by_doc, width=.5, color='steelblue')
ax2.set_xticks(range(df_topic_weightage_by_doc.index.unique().__len__()))
ax2.xaxis.set_major_formatter(tick_formatter)
ax2.set_title('Number of Documents by Topic Weightage', fontdict=dict(size=10))

EDIT: enter image description here

CodePudding user response:

You can use matplotlib.axes.Axes.set_xticklabels

For example:

import matplotlib.pyplot as plt
import numpy as np

values = np.arange(10)

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Changing the font size of x-axis ticks')
ax1.plot(values, values)
ax2.plot(values, values)

ax1.set_xticklabels(values, fontsize=16)
ax2.set_xticklabels(values, fontsize=6)

plt.show()

enter image description here

CodePudding user response:

ax1.set_xticks(range(df_dominant_topic_in_each_doc.Dominant_Topic.unique().__len__()))
tick_formatter = FuncFormatter(lambda x, pos: 'Topic '   str(x)  '\n'   df_top3words.loc[df_top3words.topic_id==x, 'words'].values[0])
ax1.xaxis.set_major_formatter(tick_formatter)
ax1.tick_params(axis="x", labelsize=3)
  • Related