[英]Altair chart save only the data to html that is actually part of the plot
When creating a chart using altair
, I like the fact that I can save it to .html
containing all the data in the plot.使用
altair
创建图表时,我喜欢可以将其保存到包含.html
中所有数据的 .html 的事实。 However, also the columns of a pd.DataFrame
that are not plotted are stored in the .html
file.但是,
pd.DataFrame
中未绘制的列也存储在.html
文件中。
I am in a situation, where my data frame has roughly 100 columns but only ~5 of them are part of the plot.我的情况是,我的数据框大约有 100 列,但其中只有约 5 列是 plot 的一部分。 Thus I'd like to be able to only save those columns in the plot that are actually used, decreasing the file size of the
.html
by a factor 20.因此,我希望能够只保存实际使用的 plot 中的那些列,从而将
.html
的文件大小减小 20 倍。
Here's an MWE:这是一个MWE:
import numpy as np
import pandas as pd
import altair as alt
mwe_df = pd.DataFrame(
{
"x": np.arange(10),
"y": np.random.randn(10),
"z": np.random.randn(10),
}
)
chart = alt.Chart(mwe_df).mark_line().encode(
x="x",
y="y"
)
chart.save("mwe.html")
The resulting file mwe.html
looks like this生成的文件
mwe.html
看起来像这样
<!DOCTYPE html>
<html
// I cut a lot of code away to show the important part
<body>
<div id="vis"></div>
<script>
(function (vegaEmbed) {
// I cut a lot of code away to show the important part
"datasets": {
"data-f268207ac55fefac2d4e472cf850ebe3": [{
"x": 0,
"y": 0.5499788508261615,
"z": -1.477946005772743 // Don't want this here
}, {
"x": 1,
"y": 0.6200551442726329,
"z": 0.857375723129614 // Don't want this here
}, {
"x": 2,
"y": 2.162997199610359,
"z": 0.8528682789795423 // Don't want this here
},
// I cut a lot of code away to show the important part
</body>
</html>
I could of course only hand a subset of the data frame to altair
, ie by calling alt.Chart(mwe_df[["x","y"]])
but this seems a bit cumbersome.我当然只能将数据帧的一个子集交给
altair
,即通过调用alt.Chart(mwe_df[["x","y"]])
但这似乎有点麻烦。
Is there a way to tell altair
to only save the relevant columns in the .html
?有没有办法告诉
altair
只保存.html
中的相关列?
I also think this would be a useful feature to have in Altair, but there are some considerations with implementing it.我也认为这将是 Altair 中的一个有用功能,但在实施它时需要考虑一些事项。 You can follow preliminary work and discussion in this PR https://github.com/altair-viz/altair/pull/2586 .
您可以在此 PR https://github.com/altair-viz/altair/pull/2586中关注初步工作和讨论。 In addition of what is mentioned there, it is possible that something might be offered via VegaFusion ( https://vegafusion.io/ ) in the future as it covers some of the same things as the altair_transform package.
除了那里提到的内容之外,将来可能会通过 VegaFusion ( https://vegafusion.io/ ) 提供某些内容,因为它涵盖了与 altair_transform package 相同的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.