-1

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

enter image description here

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. enter image description here 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 ...

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Tokeru
  • 29
  • 5
  • `ax.minorticks_off()`: [Code and plot](https://i.stack.imgur.com/xYN4x.png) – BigBen May 25 '23 at 13:59
  • Well ok this is a good hint. Wasn't aware of major/minor ticks enough. Still it doesn't solve the problem completely as I like the minor plots. I only want to turn off their labels. Is there an easy way to do it? First 5min of searching didn't reveal it. – Tokeru May 25 '23 at 14:11
  • 1
    Ah my bad. `ax.yaxis.set_minor_formatter('')` maybe? – BigBen May 25 '23 at 14:20

1 Answers1

1

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

enter image description here

Tokeru
  • 29
  • 5