简体   繁体   English

在 Python Altair 中使条形图的 x 轴标记水平或 45 度可读

[英]Make bar chart's x-axis markers horizontal or 45 degree readable in Python Altair

I am trying to create a bar chart with year data on x-axis.我正在尝试在 x 轴上创建一个带有年份数据的条形图。 It works but the year marker on x-xais are all in vertical direction and I want to make them more readable - either horizontal or 45 degree.它可以工作,但 x-xais 上的年份标记都是垂直方向的,我想让它们更具可读性 - 水平或 45 度。 I tried using the year:T in datatime format but it gave me the year and month markers (I just wanted to have the year markers on xais).我尝试以数据时间格式使用year:T ,但它给了我年份和月份标记(我只是想在 xais 上有年份标记)。 How do I make the year markers on x-axis either horizontal or 45 degree?如何使 x 轴上的年份标记水平或 45 度?

import altair as alt
from vega_datasets import data

source = data.wheat()
source.info()

abars = alt.Chart(source).mark_bar().encode(
    x='year:O',
    y="wheat:Q"
)

在此处输入图像描述

Set labelAngle :设置labelAngle

alt.Chart(source).mark_bar().encode(
    x=alt.X('year:O', axis=alt.Axis(labelAngle=-45)),
    y="wheat:Q"
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM