Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีลบตัวเลขแบบวิทยาศาสตร์ออกจาก Log-Log Plot ใน Matplotlib


ใช้คำสั่ง ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())

ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล x และ y ด้วย numpy
  • พล็อต x และ y ด้วย scatter()
  • ตั้งค่ามาตราส่วน x และ y axes ด้วย set_xscale() และ set_yscale()
  • ลบตัวเลขแบบวิทยาศาสตร์ด้วยการตั้งค่า format tick values เป็นตัวเลขธรรมดา
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

ตัวอย่างโค้ด

import numpy as np
from matplotlib import pyplot as plt, ticker as mticker

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.array([1, 7, 6, 4, 0])
y = np.array([6, 2, 3, 5, 1])
plt.scatter(y, x, c=x, cmap="copper")
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())
plt.show()

ผลลัพธ์

วิธีลบตัวเลขแบบวิทยาศาสตร์ออกจาก Log-Log Plot ใน Matplotlib