วิธีใช้มาตราส่วนลอการิทึมในแปลง seaborn


คุณสามารถใช้ฟังก์ชัน plt.xscale() และ plt.yscale() เพื่อใช้มาตราส่วนลอการิทึมสำหรับแกน x และแกน y ตามลำดับในแผนภาพทางทะเล:

 import matplotlib. pyplot as plt
import seaborn as sns

#create scatterplot with log scale on both axes
sns. scatterplot (data=df, x=' x ', y=' y ')
plt. xscale ('log')
plt. yscale ('log')

ตัวอย่างต่อไปนี้แสดงวิธีการใช้ฟังก์ชันเหล่านี้ในทางปฏิบัติ

ตัวอย่าง: การใช้มาตราส่วนลอการิทึมในพล็อต Seaborn

สมมติว่าเรามี DataFrame แพนด้าดังต่อไปนี้:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' x ': [2, 5, 6, 7, 9, 13, 14, 16, 18],
                   ' y ': [200, 1700, 2300, 2500, 2800, 2900, 3400, 3900, 11000]})

#view DataFrame
print (df)

    xy
0 2 200
1 5 1700
2 6 2300
3 7 2500
4 9 2800
5 13 2900
6 14 3400
7 16 3900
8 18 11000

เราสามารถใช้ฟังก์ชัน Scatterplot() ในทะเลเพื่อสร้าง Scatterplot ที่ใช้สเกลเชิงเส้นบนทั้งแกน X และแกน Y:

 import seaborn as sns

#create scatterplot with default axis scales
sns. scatterplot (data=df, x=' x ', y=' y ')

หากต้องการใช้มาตราส่วนลอการิทึมสำหรับแกน y เท่านั้น เราสามารถใช้ไวยากรณ์ต่อไปนี้:

 import matplotlib. pyplot as plt
import seaborn as sns

#create scatterplot with log scale on y-axis
sns. scatterplot (data=df, x=' x ', y=' y ')
plt. yscale ('log')

มาตราส่วนลอการิทึม

โปรดทราบว่าขณะนี้แกน Y ใช้มาตราส่วนลอการิทึม

เรายังสามารถใช้มาตราส่วนลอการิทึมบนแกน x ได้หากต้องการ:

 import matplotlib. pyplot as plt
import seaborn as sns

#create scatterplot with log scale on both axes
sns. scatterplot (data=df, x=' x ', y=' y ')
plt. yscale (' log ')
plt. xscale (' log ') 

โปรดทราบว่าตอนนี้ทั้งสองแกนใช้มาตราส่วนลอการิทึม

ที่เกี่ยวข้อง: เมื่อใดที่คุณควรใช้มาตราส่วนลอการิทึมในแผนภูมิ

แหล่งข้อมูลเพิ่มเติม

บทช่วยสอนต่อไปนี้จะอธิบายวิธีการทำงานทั่วไปอื่นๆ ใน Seaborn:

วิธีเพิ่มชื่อเรื่องให้กับแปลงทะเล
วิธีหมุนป้ายกำกับแกนในแปลง Seaborn
วิธีเปลี่ยนป้ายกำกับแกนบนพล็อต Seaborn

เพิ่มความคิดเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *