ฉันกำลังพยายามสร้างพล็อตแท่งที่มีถัง x-axis ความกว้างแปรผันใน Seaborn คล้ายกับแผนภูมินี้: ความกว้าง x ทั้งหมดของฉันจะเพิ่มขึ้นเป็น 100% แต่ดูเหมือนจะไม่พบตัวอย่างวิธีบรรลุเป้าหมายนี้ผ่าน Seaborn มีความคิดอะไรบ้าง?
barplot ความกว้างแปรผันพร้อมการเกิดทะเล [ซ้ำกัน]
คำตอบ (2)
อาจมีคำตอบที่เป็นไปได้หลายประการที่นี่ ใน barplot ที่เกิดในทะเล คุณสามารถใช้พารามิเตอร์สองสามตัวร่วมกันเพื่อจุดประสงค์นั้นได้: "ความกว้าง" (ค่าความกว้างของแท่ง) "ซ้าย" (ค่าตำแหน่งบนแกน x ซึ่งเป็นอาร์กิวเมนต์บังคับ) และ "จัดแนว ".
ตัวอย่างง่ายๆ:
import seaborn as sns
data = [7, 3, 15]
widths = [1, 5, 3]
left = [0, 1, 6]
sns.plt.bar(left, data, width = widths, color=('orange','green','blue'),
alpha = 0.6, align='edge', edgecolor = 'k', linewidth = 2)
โปรดทราบว่า "ซ้าย" (ตำแหน่งของแท่ง) ควรสอดคล้องกับความกว้างเพื่อให้แท่งสัมผัสกันและไม่ทับซ้อนกัน
person
BossaNova
schedule
17.10.2017
เมื่อใช้
sns.plt.bar
คุณกำลังเรียกใช้ฟังก์ชัน matplotlibs plt.bar
สิ่งนี้ไม่เกี่ยวข้องกับสัตว์ทะเล
- person ImportanceOfBeingErnest; 18.10.2017
หากคุณต้องการใช้ Seaborn สำหรับ barplot คุณจะต้องเปลี่ยนความกว้างของแถบสี่เหลี่ยม (แพตช์) หลังจากนั้น (ทำได้ผ่านอินเทอร์เฟซเชิงวัตถุ matplotlib ตาม คำตอบนี้):
import seaborn as sns
iris = sns.load_dataset('iris')
ax = sns.barplot('species', 'sepal_width', data=iris)
widthbars = [0.3, 0.6, 1.2]
for bar, newwidth in zip(ax.patches, widthbars):
x = bar.get_x()
width = bar.get_width()
centre = x + width/2.
bar.set_x(centre - newwidth/2.)
bar.set_width(newwidth)
คุณสามารถสร้าง barplot ที่คล้ายกันใน matplotlib ได้โดยตรง:
import matplotlib.pyplot as plt
widths = [0.3, 0.6, 1.2]
for x_pos, (species_name, species_means) in enumerate(iris.groupby('species').mean().groupby('species')):
plt.bar(x_pos, species_means['sepal_width'], widths[x_pos])
person
joelostblom
schedule
18.10.2017
pyplot.bar
แทน - person ImportanceOfBeingErnest   schedule 18.10.2017