I am trying to render specified ticks on the y axis using matplotlib
Python package. The library for some reason renders more ticks than I give it to render and use scientific notation for them. For me it seems like a bug in the library, I was able to reproduce the issue on more devices. Is there a workaround?
The source code to reproduce the issue:
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
plt.figure(figsize=(6,4))
plt.xlim(0, 1000)
plt.xlabel('x')
plt.yscale('log')
plt.ylim(20, 200)
plt.yticks([20, 28, 39, 54, 75, 110, 150, 200])
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter(useOffset=False))
plt.gca().get_yaxis().set_tick_params(which='minor', width=0)
plt.ylabel('y')
plt.savefig('test.png')
plt.close()
The rendered graph:
EDIT Calling plt.gca().get_yaxis().clear()
after the plt.yscale('log')
seems to solve the issue.
Commenting plt.yscale('log')
out, seems to remove scientific notation from the y axis while keeping the desired tick values.
If you still need it, putting plt.yscale('log')
after plt.ylim()
and plt.yticks()
seems to work:
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
plt.figure(figsize=(6,4))
plt.xlim(0, 1000)
plt.xlabel('x')
plt.ylim(20, 200)
plt.yticks([20, 28, 39, 54, 75, 110, 150, 200])
plt.yscale('log')
plt.gca().get_yaxis().set_major_formatter(mticker.ScalarFormatter(useOffset=False))
plt.gca().get_yaxis().set_tick_params(which='minor', width=0)
plt.ylabel('y')
plt.savefig('test.png')
plt.close()
To replace 100 for 10^2, I commented the first plt.gca()
line:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.