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

การเปลี่ยนสีของ X-axis Tick Label เดียวใน Matplotlib

ในการเปลี่ยนสีของ X-axis tick label เดียวใน matplotlib เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่
  • เพิ่ม '~.axes.Axes' ให้กับ figure เป็นส่วนหนึ่งของ subplot arrangement
  • สร้างจุดข้อมูล x และ y ด้วย numpy
  • พลอต x และ y ด้วยเมธอด plot()
  • เพื่อตั้งค่าสีของ X-axis tick label ใน matplotlib เราสามารถใช้เมธอด tick_params() พร้อม axis='x' และ color='red'
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import numpy as np
from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot(111)

x = np.linspace(-10, 10, 100)
y = np.sin(x)
plt.plot(x, y)
ax.tick_params(axis='x', colors='red')
plt.show()

ผลลัพธ์

การเปลี่ยนสีของ X-axis Tick Label เดียวใน Matplotlibการเปลี่ยนสีของ X-axis Tick Label เดียวใน Matplotlib