【问题标题】:Reducing number of bins for subplots of log-log in matplotlib减少 matplotlib 中 log-log 子图的 bin 数量
【发布时间】:2020-11-05 22:00:56
【问题描述】:

我正在尝试使用代码生成以下情节:

import numpy as np
import matplotlib.pyplot as plt
from mpltools import annotation
import matplotlib.ticker as ticker

h = np.array([1.44, 0.72, 0.36, 0.18])

m2_velocity_2 = np.array([0.088373, 0.0440217, 0.0149557, 0.00429101])
m2_pressure_2 = np.array([1.62808, 0.741043, 0.244384, 0.069385])

fig = plt.figure(figsize=(10, 5))

ax1 = plt.subplot(121)
ax1.loglog(h, m2_velocity_2, '-*', label='weight = 2')
annotation.slope_marker((0.4, 0.012), (2, 1), ax=ax1)
ax1.xaxis.set_major_locator(ticker.MaxNLocator(nbins=3))
ax1.set_title("Velocity")
ax1.set_xlabel("Average particle spacing")
ax1.set_ylabel("RMS error")
ax1.grid()

ax2 = plt.subplot(122)
ax2.loglog(h, m2_pressure_2, '-*', label='weight = 2')
annotation.slope_marker((0.4, 0.20), (2, 1), ax=ax2)
ax2.set_title("Pressure")
ax2.set_xlabel("Average particle spacing")
ax2.set_ylabel("RMS error")
ax2.grid()

fig.tight_layout()
plt.show()

这在轴上产生了非常紧密的刻度。我已经尝试过thisthis 的方法,但它似乎并没有清理轴。附上上面代码生成的图:

有什么建议吗?

【问题讨论】:

  • rotation=45 代替复杂的手动代码/标签设置怎么样?
  • @Mr.T 我正在尝试解决我的审稿人所说的问题,而不会造成更多麻烦,所以可能会再保存一次......

标签: python python-3.x matplotlib


【解决方案1】:

我同意@Mr.T 的观点,即最好的方法就是旋转标签。也许一种替代方法是将替代标签向下移动?

for xtl in ax1.xaxis.get_minorticklabels()[1::2]:
    xtl.set_position((xtl.get_position()[0], xtl.get_position()[1]-0.05))

这远非理想,因为您的可见刻度标签有点不稳定,但这是一个快速修复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多