用于 logit 刻度轴标签的 Python matplotlib ValueError

标签 python matplotlib label

在使用 matplotlib 为 logit 刻度轴设置标签时,我遇到了标签定位错误,这是我的示例:

import matplotlib.pyplot as plt;
from matplotlib.ticker import FormatStrFormatter;

x = [1,2,3,4,5];
y = [0.1,0.2,0.3,0.4,0.5];

fig,ax = plt.subplots();

ax.set_ylim([0.005,0.99]);
ax.set_yscale('logit');
ax.yaxis.set_minor_formatter(FormatStrFormatter(""));   

ax.set_xlabel("X axis");
ax.set_ylabel("Y axis"); #set y axis label (logit scale)

ax.plot(x,y);
plt.show();
plt.close();

这是痕迹:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 136, in _draw
    self.figure.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/figure.py", line 1143, in draw
    renderer, self, dsu, self.suppressComposite)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2409, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axis.py", line 1150, in draw
    self.label.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/text.py", line 762, in draw
    raise ValueError("posx and posy should be finite values")
ValueError: posx and posy should be finite values

当我省略时效果很好

ax.set_ylabel("Y axis"); #set y axis label (logit scale)

标签设置在轴刻度之前或之后似乎无关紧要。 有人知道如何解决这个问题吗?

最佳答案

文本标签的设置方式存在错误,这会导致 logit 量表出现非数字边界框,请参阅 this issue on GitHub .

解决方法是添加

ax.spines['left']._adjust_location()

(其中 'left' 可以替换为 'right''top''bottom' 取决于人们感兴趣的轴)在代码中。

完整示例:

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [0.05,0.2,0.3,0.4,0.95]

fig,ax = plt.subplots()

ax.set_yscale('logit')

ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.spines['left']._adjust_location()

ax.plot(x,y)
plt.show()

enter image description here

关于用于 logit 刻度轴标签的 Python matplotlib ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553735/

相关文章:

d3.js - 为什么转换会从我的标签中删除文本?

python - 列表到字典 - 改进?

python - Matplotlib 奇子图

Python:如何识别字符串中的十进制数字?

python - matplotlib如何将每个子图x标签,y标签设置在一起

swift - 没有在最后一个 ScoreViewController Swift 上显示分数

python - 使用 youtube 的 API 在播放列表中插入视频时的 Oauth2.json 文件

python - Matplotlib 辅助/双轴 - 用圆圈和箭头标记 - 用于黑白 (bw) 发布

python - 颜色条相对于地理轴的正确放置(cartopy)

ios - CPTBarPlot (Core-Plot) 上的定位标签