【问题标题】:How do I change the label font size using Matplotlib, Tinker, and SubplotHost?如何使用 Matplotlib、Tinker 和 SubplotHost 更改标签字体大小?
【发布时间】:2020-12-06 01:59:30
【问题描述】:

请注意,我正在制作一个带有嵌套轴标签的图。无论我尝试什么,字体大小都保持不变。理想情况下,我希望能够为每组轴标签赋予自己的颜色和字体大小。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.axisartist.parasite_axes import SubplotHost

fig1 = plt.figure()
ax1 = SubplotHost(fig1, 111)
fig1.add_subplot(ax1)

# Some data
x = np.arange(1,6)
y = np.random.random(len(x))

# First X-axis
ax1.plot(x, y)
ax1.set_xticks(x)
ax1.set_xticklabels(['7up', 'cream', 'coke', 'rootb', 'cherryc'])
#ax1.xaxis.set_label_text('First X-axis') # Uncomment to label axis
ax1.yaxis.set_label_text("Sample data")

# Second X-axis
ax2 = ax1.twiny()
offset = 0, -25 # Position of the second axis
new_axisline = ax2.get_grid_helper().new_fixed_axis
ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset)
ax2.axis["top"].set_visible(False)

ax2.set_xticks([0.0, 0.6, 1.0])
ax2.xaxis.set_major_formatter(ticker.NullFormatter())
ax2.xaxis.set_minor_locator(ticker.FixedLocator([0.3, 0.8]))
ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(['weekly', 'daily']))

# Third X-axis
ax3 = ax1.twiny()
offset = 0, -50
new_axisline = ax3.get_grid_helper().new_fixed_axis
ax3.axis["bottom"] = new_axisline(loc="bottom", axes=ax3, offset=offset)
ax3.axis["top"].set_visible(False)

ax3.set_xticks([0.0, 1.0])
ax3.xaxis.set_major_formatter(ticker.NullFormatter())
ax3.xaxis.set_minor_locator(ticker.FixedLocator([0.5]))
ax3.xaxis.set_minor_formatter(ticker.FixedFormatter(['customer favs']))

plt.show()

【问题讨论】:

  • 尝试以这种方式改变你的轴颜色:在分配每个轴之前设置不同的matplotlib.rcParams['text.color']值。如果你这样做,你会发现你的ax1 有点复杂,不像另外两个:它包含xaxisyaxis。希望这很有用。

标签: python numpy matplotlib data-visualization


【解决方案1】:

这不是一个单独的设置,但都可以在“xticks.labelsize”中进行配置。它们是:'xx-small、x-small、small、medium、medium、large、x-large 和 xx-large,'

plt.rcParams['xtick.labelsize'] = 'XX-large'

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2021-07-19
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多