y轴刻度以对数格式标记,但它们不应该被标记。

huangapple go评论69阅读模式
英文:

y-ticks in log format are being labels when they shouldn't be

问题

I want to plot two lines with matplotlib / seaborn and a logarithmic y-scale.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data1 = pd.Series([0.5, 1.6])
data2 = pd.Series([0.5, 1.1])
f, ax = plt.subplots()
sns.lineplot(data1)
sns.lineplot(data2)
ax.set_yscale("log")

Ok, I have two things here that I don't want. First, having 1 and 0.6 written the scientific way isn't great, and second, I actually want 0.5 to be labeled rather than 0.6. Sounds like an easy question so far with tons of solutions on stackoverflow. I tried several of them, for example:

import matplotlib.ticker as ticker
ax.set_yticks([0.5, 1, 1.5])
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%.1f"))

I also tried the scalar formatter, the fixed locator, and a lot more options.

Now to my real problem: whatever I try, the 6x10^-1 isn't changing. When I look for the ticks to be labeled, 0.6 shouldn't be labeled actually. It seems to be some automatic label because matplotlib thinks "ok, this is somehow the end of the scale, it should better be labeled anyway." While this automatism seems to stand outside of the formatting mechanics. The only way I found to change is to manually label 0.6, but I don't want to label it.

英文:

I want to plot two lines with matplotlib / seaborn and a logarithmic y-scale.

pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

data1=pd.Series([0.5,  1.6])
data2=pd.Series([0.5,  1.1 ])
f,ax=plt.subplots()
sns.lineplot(data1)
sns.lineplot(data2)
ax.set_yscale("log")

y轴刻度以对数格式标记,但它们不应该被标记。

Ok I have two things here, that I dont want. First is having 1 and 0.6 written the scientific way isnt great and second is I actually want 0.5 to be labeled rather than 0.6. Sounds like an easy question so far with tons of solutions on stackoverflow. I tried several of them for example

import matplotlib.ticker as ticker
ax.set_yticks([0.5,1,1.5])
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%.1f"))

I also tried the scalar formatter, the fixed locator and a lot more options.
y轴刻度以对数格式标记,但它们不应该被标记。
Now to my real problem: whatever I try the 6x10-1 isn't changing. When I look for the ticks to be labeled 0.6 shouldn't be labeled actually. It seems to be some automatic label because matplotlib thinks "ok this is somehow the end of the scale, it should better be labeled anyway". While this automatism seems to stand outside of the formatting mechanics. The only way I found to change is to manually label 0.6, but I don't want to label it ...

答案1

得分: 1

好的,感谢BigBen的答案,我得到了正确的提示。我没有意识到标签的格式分为主要和次要刻度。

我用以下代码解决了我的问题:

ax.yaxis.set_minor_locator(ticker.MultipleLocator(base=0.1))
ax.yaxis.set_minor_formatter(ticker.FormatStrFormatter(""))

y轴刻度以对数格式标记,但它们不应该被标记。

英文:

Ok thanks to the answers of BigBen I got the right hint. I wasn't aware that label formatting is divided in major and minor ticks as well.
I solved my problem with

ax.yaxis.set_minor_locator(ticker.MultipleLocator(base=0.1))
ax.yaxis.set_minor_formatter(ticker.FormatStrFormatter(""))

y轴刻度以对数格式标记,但它们不应该被标记。

huangapple
  • 本文由 发表于 2023年5月25日 21:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定