Để giảm mật độ x-ticks trong Seaborn, ta có thể dùng set_visible=False cho các vị trí lẻ.
Các bước thực hiện
- Thiết lập kích thước hình và khoảng đệm.
- Tạo DataFrame với khóa X-axis và Y-axis.
- Vẽ biểu đồ cột với barplot() hiển thị ước lượng điểm và khoảng tin cậy.
- Lặp qua bar_plot.get_xticklabels(). Nếu index chẵn thì hiển thị, lẻ thì ẩn.
- Hiển thị hình bằng show().
Ví dụ
import pandas
import matplotlib.pylab as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pandas.DataFrame({"X-Axis": [i for i in range(10)], "Y-Axis": [i for i in range(10)]})
bar_plot = sns.barplot(x='X-Axis', y='Y-Axis', data=df)
for index, label in enumerate(bar_plot.get_xticklabels()):
if index % 2 == 0:
label.set_visible(True)
else:
label.set_visible(False)
plt.show()
Kết quả
