简体   繁体   English

Altair 图表仅将数据保存到 html 这实际上是 plot 的一部分

[英]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.

相关问题 Altair 交互式图表:显示所有轴标签,而 transform_filter 仅获取部分数据 - Altair interactive chart: Show all axis labels while transform_filter only takes part of the data Altair scatter plot 到条形图单 select 行数据框 - Altair scatter plot to bar chart single select row of data frame 如何在Python Altair Chart.save(file.html)生成的Altair Chart的html文件上显示默认工具提示? - How to display the default tooltip on the html file of a n Altair Chart generated by Python Altair Chart.save(file.html)? 如何在altair中显示部分重复图表 - How to show part of the repeated chart in altair Altair - 在分面图中绘制圆、椭圆、注释 - Altair - plot circle, ellipse, annotations in faceted chart Altair 使用 selenium chromedriver 自动保存图表 - Altair automatic save chart using selenium chromedriver 在 google colab 中更改 Altair 图表的“另存为”文件名 - Changing the "save as" filename for Altair chart in google colab Altair 图书馆 | 难以在 .png 中保存图表 - Altair library | Difficulty to save a Chart in .png 使用交互式 plot 在 Altair 中仅缩放一个轴 - Scale only one axis in Altair with interactive plot 保存为 HTML 时无法呈现 Altair 图表 - Unable to render Altair chart when saved as HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM