英文:
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")
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.
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(""))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论