Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在logscale方框图中设置y记号:Matplotlib_Python_Matplotlib_Boxplot_Ticker - Fatal编程技术网

Python 在logscale方框图中设置y记号:Matplotlib

Python 在logscale方框图中设置y记号:Matplotlib,python,matplotlib,boxplot,ticker,Python,Matplotlib,Boxplot,Ticker,我正在以对数比例绘制0到20范围内的一些数据的箱线图。我想设置一个自定义的开始和结束刻度。所有其他刻度也应为对数刻度(2*10^-1) 我尝试使用ax.yaxis.set\u major\u格式化程序(ticker.FuncFormatter(lambda y,{:g}.format(y)) 不起作用 也 发出警告: Invalid limit will be ignored. plt.ylim(ymin, ymax) 以下是完整的代码: import numpy as np import

我正在以对数比例绘制0到20范围内的一些数据的箱线图。我想设置一个自定义的开始和结束刻度。所有其他刻度也应为对数刻度(2*10^-1)

我尝试使用
ax.yaxis.set\u major\u格式化程序(ticker.FuncFormatter(lambda y,{:g}.format(y))
不起作用

发出警告:

Invalid limit will be ignored.
  plt.ylim(ymin, ymax)
以下是完整的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
from math import log, exp

log_scale = True
min = 0
max = 50

atributes = []
for i in [8, 10, 6, 12]:
    att = [i] * 50
    atributes.extend(att)
values = np.random.uniform(1, 20, 200)

df1 = pd.DataFrame({'Attributes': atributes, 'values': values})
if min is None and max is not None:
    min = df1["values"].min()
if max is None and min is not None:
    max = df1["values"].max()

if min or max is not None:
    df1 = df1[(min <= df1["values"]) & (df1["values"] <= max)]

plt.figure()
sns.set_style("whitegrid")
ax = None

ax = sns.boxplot(x='Attributes', y='values', data=df1, color="white")

if log_scale:
    plt.yscale('log')
    ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y)))

if min is not None:
    try:
        min, max = log(min), log(max)
    except ValueError:
        min = 0
        max = log(max)
    ymin = min - 0.1 * (max - min)
    ymax = max + 0.1 * (max - min)
    plt.ylim(exp(ymin), exp(ymax))

fig = plt.gcf()
fig.set_size_inches(10.5, 8)
plt.show()

将numpy导入为np
作为pd进口熊猫
将matplotlib.pyplot作为plt导入
导入seaborn作为sns
从matplotlib导入代码
从数学导入日志,exp
对数刻度=真
最小值=0
最大值=50
心房肌=[]
因为我在[8,10,6,12]中:
附件=[i]*50
心房扩大(附件)
数值=np.随机.均匀(1,20,200)
df1=pd.DataFrame({'Attributes':atributes,'values':values})
如果“最小”为“无”,而“最大”为“无”:
min=df1[“值”].min()
如果“最大”为“无”,而“最小”为“无”:
max=df1[“值”].max()
如果“最小”或“最大”不是“无”:

df1=df1[(min
ymin=min-0.1*(max-min)
当这使
ymin
为零或为负时,对数刻度将有问题。您可能需要取
min
max
log
,进行计算,然后调用
exp()
。此外,
设置主格式化程序…
不起作用对您遇到的问题不是很有帮助的解释。可能还需要
set\u minor\u formatter
来显示小刻度。您能创建一个可复制的示例吗?我已经更新了代码。我也尝试过实现您提到的内容,但没有多大区别!但是为什么要为日志sca设置
min=0
le?
log(0)
是负无穷大。将其设置为
1
0.5
怎么样?设置min=0后,我不取min的log,以避免出现数学错误。是否添加了
ax.yaxis.set_minor_格式化程序(ticker.FuncFormatter(lambda y,{:g}.format(y))
以同时显示次刻度?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import ticker
from math import log, exp

log_scale = True
min = 0
max = 50

atributes = []
for i in [8, 10, 6, 12]:
    att = [i] * 50
    atributes.extend(att)
values = np.random.uniform(1, 20, 200)

df1 = pd.DataFrame({'Attributes': atributes, 'values': values})
if min is None and max is not None:
    min = df1["values"].min()
if max is None and min is not None:
    max = df1["values"].max()

if min or max is not None:
    df1 = df1[(min <= df1["values"]) & (df1["values"] <= max)]

plt.figure()
sns.set_style("whitegrid")
ax = None

ax = sns.boxplot(x='Attributes', y='values', data=df1, color="white")

if log_scale:
    plt.yscale('log')
    ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y)))

if min is not None:
    try:
        min, max = log(min), log(max)
    except ValueError:
        min = 0
        max = log(max)
    ymin = min - 0.1 * (max - min)
    ymax = max + 0.1 * (max - min)
    plt.ylim(exp(ymin), exp(ymax))

fig = plt.gcf()
fig.set_size_inches(10.5, 8)
plt.show()