Altair Y 轴标签左对齐和间距编辑可能吗?
Altair Y-Axis Label Left-Alignment & Spacing Edits Possible?
我正在尝试在 Altair 中重新创建此图表,但不知道如何进行一些小的编辑:https://datawrapper.dwcdn.net/E0jaK/1/
这是我目前的进度:https://imgur.com/RMuTU7h
我想找出的三个编辑是:
如何左对齐 y 轴标签
如何在标签和 y 轴之间添加间距
如何将条形图编号标签移动到条形图的底部而不是末端
任何关于如何实施这些编辑的见解都会很棒。
原始数据如下:
Crime,Count
Larceny/Theft,739
Assault,177
Burglary/Breaking & Entering,133
Destruction of Property/Vandalism,128
Drugs,107
Motor Vehicle Theft,95
Fraud,71
Sex Offenses,57
Suspicious Activity,45
Trespassing,23
Family Offenses,22
Weapons Violations,21
这是我用的主题:
def chart_theme():
font = "Calibri"
return {
"width": 700,
"height": 300,
"background": "white",
"config": {
"title": {
"fontSize": 20,
"anchor": "start"
},
"axisY": {
"labelFont": font,
"labelFontSize": 13,
"labelLimit":200,
"ticks": False,
"titleFont": font,
"titleFontSize": 12,
"titleAlign":"right",
"titleAngle": 0,
"titleY": -10,
"titleX": 25,
},
"view":{"stroke": "transparent",
},
}
}
这是图表代码:
base = alt.Chart(df, title='San Francisco Crime (11/05 - 11/11)').encode(
x=alt.X('Count', axis=None),
y=alt.Y('Crime', title='Crimes reported to SFPD, by top complaint',
sort=list(df.Crime.values)))
bars = base.mark_bar(size=22).encode(color=alt.condition(
alt.datum.Count > 700,
alt.value('#e17700'),
alt.value('#00648e')))
text = base.mark_text(
color='white',
align='right',
size=12,
dx=-3
).encode(
text='Count')
chart = bars + text
chart
任何 guidance/suggestions 将不胜感激。
这样的怎么样
import pandas as pd
import altair as alt
data = {'Larceny/Theft':739,
'Assault':177,
'Burglary/Breaking & Entering':133,
'Destruction of Property/Vandalism':128,
'Drugs':107,
'Motor Vehicle Theft':95,
'Fraud':71,
'Sex Offenses':57,
'Suspicious Activity':45,
'Trespassing':23,
'Family Offenses':22,
'Weapons Violations':21}
df = pd.DataFrame(list(data.items()), columns=['Crime', 'Count'])
base = alt.Chart(df, title='San Francisco Crime (11/05 - 11/11)').encode(
x=alt.X('Count', axis=None),
y=alt.Y('Crime', title='Crimes reported to SFPD, by top complaint',
sort=list(df.Crime.values)))
bars = base.mark_bar(size=22).encode(color=alt.condition(
alt.datum.Count > 700,
alt.value('#e17700'),
alt.value('#00648e')))
text = alt.Chart(df).mark_text(
color='white',
align='left',
x=3
).encode(alt.Text('Count:N'), alt.Y('Crime', sort=list(df.Crime.values)))
chart = bars + text
chart.properties(width=700).configure_axisY(
titleAngle=0,
titleY=-10,
titleX=-60,
labelPadding=160,
labelAlign='left'
)
我正在尝试在 Altair 中重新创建此图表,但不知道如何进行一些小的编辑:https://datawrapper.dwcdn.net/E0jaK/1/
这是我目前的进度:https://imgur.com/RMuTU7h
我想找出的三个编辑是:
如何左对齐 y 轴标签
如何在标签和 y 轴之间添加间距
如何将条形图编号标签移动到条形图的底部而不是末端
任何关于如何实施这些编辑的见解都会很棒。
原始数据如下:
Crime,Count
Larceny/Theft,739
Assault,177
Burglary/Breaking & Entering,133
Destruction of Property/Vandalism,128
Drugs,107
Motor Vehicle Theft,95
Fraud,71
Sex Offenses,57
Suspicious Activity,45
Trespassing,23
Family Offenses,22
Weapons Violations,21
这是我用的主题:
def chart_theme():
font = "Calibri"
return {
"width": 700,
"height": 300,
"background": "white",
"config": {
"title": {
"fontSize": 20,
"anchor": "start"
},
"axisY": {
"labelFont": font,
"labelFontSize": 13,
"labelLimit":200,
"ticks": False,
"titleFont": font,
"titleFontSize": 12,
"titleAlign":"right",
"titleAngle": 0,
"titleY": -10,
"titleX": 25,
},
"view":{"stroke": "transparent",
},
}
}
这是图表代码:
base = alt.Chart(df, title='San Francisco Crime (11/05 - 11/11)').encode(
x=alt.X('Count', axis=None),
y=alt.Y('Crime', title='Crimes reported to SFPD, by top complaint',
sort=list(df.Crime.values)))
bars = base.mark_bar(size=22).encode(color=alt.condition(
alt.datum.Count > 700,
alt.value('#e17700'),
alt.value('#00648e')))
text = base.mark_text(
color='white',
align='right',
size=12,
dx=-3
).encode(
text='Count')
chart = bars + text
chart
任何 guidance/suggestions 将不胜感激。
这样的怎么样
import pandas as pd
import altair as alt
data = {'Larceny/Theft':739,
'Assault':177,
'Burglary/Breaking & Entering':133,
'Destruction of Property/Vandalism':128,
'Drugs':107,
'Motor Vehicle Theft':95,
'Fraud':71,
'Sex Offenses':57,
'Suspicious Activity':45,
'Trespassing':23,
'Family Offenses':22,
'Weapons Violations':21}
df = pd.DataFrame(list(data.items()), columns=['Crime', 'Count'])
base = alt.Chart(df, title='San Francisco Crime (11/05 - 11/11)').encode(
x=alt.X('Count', axis=None),
y=alt.Y('Crime', title='Crimes reported to SFPD, by top complaint',
sort=list(df.Crime.values)))
bars = base.mark_bar(size=22).encode(color=alt.condition(
alt.datum.Count > 700,
alt.value('#e17700'),
alt.value('#00648e')))
text = alt.Chart(df).mark_text(
color='white',
align='left',
x=3
).encode(alt.Text('Count:N'), alt.Y('Crime', sort=list(df.Crime.values)))
chart = bars + text
chart.properties(width=700).configure_axisY(
titleAngle=0,
titleY=-10,
titleX=-60,
labelPadding=160,
labelAlign='left'
)