admin管理员组文章数量:1435859
I am trying to place two horizontal bar charts side by side, each proceeding in the opposite direction, as in this example. However, as can be seen in the example, and in the test case below, horizontal bars seem to be offset by 1 pixel to the left of the grid, regardless of which direction they proceed in. This makes for unsatisfyingly asymmetrical visualizations, in which bars proceeding to the left do not the right-oriented y-axis they sit on, while bars proceeding to the right overlap the left-oriented axis they sit on.
import pandas as pd
import altair as alt
data = pd.DataFrame(data={
'A': ['Item 1','Item 2','Item 3'],
'B': [1,2,3],
})
left = alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(orient='right'),
alt.X('B:Q').scale(reverse=True),
).properties(title='Bars do not overlap right-oriented y-axis')
right = alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(offset=1),
alt.X('B:Q').axis(tickOffset=-1, gridDashOffset=10),
).properties(title='Bars do overlap left-oriented y-axis')
left | right
enter image description here
I would expect the left and right charts to be symmetrical. If there is a good reason for this difference in bar alignment, I would like to offset the bars in the right example by one pixel to the right with respect to the grid, so that they do not overlap the y-axis.
I have tried to achieve this effect by tweaking the offset of the y-axis in the right chart, as below. However, this results in a 1-pixel gap between the zero point on the x-axis, the bounding box, and the grid. Trying to fix these multiplies the offset issues. I simply want to offset the bars one pixel to the right, leaving the rest of the axes unchanged.
alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(offset=1),
alt.X('B:Q'),
)
I am trying to place two horizontal bar charts side by side, each proceeding in the opposite direction, as in this example. However, as can be seen in the example, and in the test case below, horizontal bars seem to be offset by 1 pixel to the left of the grid, regardless of which direction they proceed in. This makes for unsatisfyingly asymmetrical visualizations, in which bars proceeding to the left do not the right-oriented y-axis they sit on, while bars proceeding to the right overlap the left-oriented axis they sit on.
import pandas as pd
import altair as alt
data = pd.DataFrame(data={
'A': ['Item 1','Item 2','Item 3'],
'B': [1,2,3],
})
left = alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(orient='right'),
alt.X('B:Q').scale(reverse=True),
).properties(title='Bars do not overlap right-oriented y-axis')
right = alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(offset=1),
alt.X('B:Q').axis(tickOffset=-1, gridDashOffset=10),
).properties(title='Bars do overlap left-oriented y-axis')
left | right
enter image description here
I would expect the left and right charts to be symmetrical. If there is a good reason for this difference in bar alignment, I would like to offset the bars in the right example by one pixel to the right with respect to the grid, so that they do not overlap the y-axis.
I have tried to achieve this effect by tweaking the offset of the y-axis in the right chart, as below. However, this results in a 1-pixel gap between the zero point on the x-axis, the bounding box, and the grid. Trying to fix these multiplies the offset issues. I simply want to offset the bars one pixel to the right, leaving the rest of the axes unchanged.
alt.Chart(data).mark_bar().encode(
alt.Y('A:N').axis(offset=1),
alt.X('B:Q'),
)
Share
Improve this question
edited Nov 15, 2024 at 22:25
jonrsharpe
122k30 gold badges268 silver badges476 bronze badges
asked Nov 15, 2024 at 21:57
Zac ColemanZac Coleman
132 bronze badges
1 Answer
Reset to default 0You could use the offset parameter:
right = alt.Chart(data).mark_bar(xOffset=1).encode(
alt.Y('A:N'),
alt.X('B:Q'),
)
right
However, this really looks like a bug in Vega-Lite, and the default behavior should be the same for both charts (the correct behavior is the non-overlapping, which is also the case for vertical bars from bottom to top, but the same issue is seen when the bars go from top to bottom). Could you file an issue at https://github/vega/vega-lite/issues with your example? (if you are interested in exploring more what goes wrong, you could open the chart in the vega editor and inspect the compiled vega spec)
本文标签: pythonBars in bar charts aligned differently based on axis directionStack Overflow
版权声明:本文标题:python - Bars in bar charts aligned differently based on axis direction - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745669165a2669465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论