用于logit缩放轴标签的Python matplotlib ValueError [英] Python matplotlib ValueError for logit scale axis label

查看:214
本文介绍了用于logit缩放轴标签的Python matplotlib ValueError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting what looks like a label positioning error when setting a label for a logit scale axis using matplotlib, here's my example:

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)

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

It doesn't seem to matter of the label is set before or after the axis scale. Anyone know how to work around this?

推荐答案

设置文本标签的方式存在错误,这会导致logit刻度的非数字边界框,请参见

There is a bug in the way the text label is set, which results in a non-numeric bounding box for logit scales, see this issue on GitHub.

解决方案是添加

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

代码中的

(其中'left'可以由'right''top''bottom'替换,具体取决于所关注的轴).

(where 'left' can be replaced by 'right', 'top', 'bottom' depending on the axis one is interested in) in the code.

完整示例:

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()

这篇关于用于logit缩放轴标签的Python matplotlib ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆