由于某些原因,当使用altair绘图时,Y轴似乎是颠倒的(预计值从绘图的较低(底部)到较高(顶部))。此外,我希望能够改变滴答声的频率。在旧版本中,我可以使用ticks=n_ticks,但现在这个参数似乎只能接受布尔值。谢谢
import altair as alt
alt.renderers.enable('notebook')
eff_metals = pd.read_excel(filename, sheet_name='summary_eff_metals')
points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
scale=alt.Scale(zero=False, padding=50)),
y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
scale=alt.Scale(zero=False, padding=1)),
color=alt.Color('Element:N'),
)
text = points.mark_text(align='right', dx=0, dy=-5).encode(
text='Element:N'
)
chart = alt.layer(points, text, data=eff_metals,
width=600, height=300)
chart下图是:

发布于 2018-04-15 20:07:13
我没有你的数据,所以很难写出工作代码。
但这里有一个带有附加刻度的反转刻度示例,它扩展了simple scatter plot示例。请参见织女星编辑器中的here。
import altair as alt
from vega_datasets import data
iris = data.iris()
alt.Chart(iris).mark_point().encode(
x='petalWidth',
y=alt.Y('petalLength', scale=alt.Scale(domain=[7,0]), axis=alt.Axis(tickCount=100)),
color='species'
).interactive()这可能适用于您的数据:
eff_metals = pd.read_excel(filename, sheet_name='summary_eff_metals')
points = alt.Chart(eff_metals, height=250, width=400).mark_circle().encode(
x=alt.X('Temperature:Q',axis=alt.Axis(title='Temperature (°C)'),
scale=alt.Scale(zero=False, padding=50)),
y=alt.Y('Efficiency:N',axis=alt.Axis(title='Efficiency (%)'),
scale=alt.Scale(zero=False, padding=1, domain=[17,1])),
color=alt.Color('Element:N'),
)
text = points.mark_text(align='right', dx=0, dy=-5).encode(
text='Element:N'
)
chart = alt.layer(points, text, data=eff_metals,
width=600, height=300)
chart但是,我认为您可能只是在效率变量上使用了错误的type。你可以试着用‘’Efficiency:q‘替换'Efficiency:N',这可能行得通吗?
发布于 2022-02-28 14:02:23
虽然可以手动反转domain,但这需要对边界进行硬编码。
相反,我们可以只将Scale(reverse=True)传递给axis编码,例如:
from vega_datasets import data
alt.Chart(data.wheat().head()).mark_bar().encode(
x='wheat:Q',
y=alt.Y('year:O', scale=alt.Scale(reverse=True)),
)在这里,它被传递给alt.Y,因此年份(左)与默认的y='year:O' (右)相反:

https://stackoverflow.com/questions/49817153
复制相似问题