2

我想将 a 保存matplotlib plt到我也将字符串放入的 HTML 文件中。例如,HTML 文件将是这样的:

import numpy as np
import matplotlib.pyplot as plt

htmlFile = open("test.html","w")
htmlFile.write("Testing file")

然后,我想在该字符串的下一行添加一个图。例如,这个情节:

thisPlot = plt.plot(1)

我知道savefig但想不出如何让它发挥作用。

4

1 回答 1

3

你可能想给一个机会bokeh.plotting。例如:

from math import sin, pi
from bokeh.plotting import line
from bokeh.resources import CDN
from bokeh.embed import file_html

x = range(100)
y = [sin(2.*pi*(1./25.)*x_) for x_ in x]
plot = line(x, y, color="red")
html = file_html(plot, CDN, "Sine")

它将生成以下html文件:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Sine</title>

  <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh-0.6.1.min.css" type="text/css" />
  <script type="text/javascript" src="http://cdn.bokeh.org/bokeh-0.6.1.min.js"></script>
  <script type="text/javascript">
    Bokeh.set_log_level("info");
  </script>

  <script type="text/javascript">
    $(function() {
      var modelid = "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67";
      var modeltype = "Plot";
      var elementid = "68e7fcd1-5f9c-4ac7-990b-09a0a56790fc";
      Bokeh.logger.info("Realizing plot:")
      Bokeh.logger.info(" - modeltype: Plot");
      Bokeh.logger.info(" - modelid: d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67");
      Bokeh.logger.info(" - elementid: 68e7fcd1-5f9c-4ac7-990b-09a0a56790fc");
      var all_models = [{
        "attributes": {
          "tags": [],
          "doc": null,
          "tool": {
            "type": "BoxSelectTool",
            "id": "b8a71ddd-6f4f-414a-a5dc-63fec650e9c8"
          },
          "id": "37457aff-27fc-48f1-b051-2613e9d781d1"
        },
        "type": "BoxSelection",
        "id": "37457aff-27fc-48f1-b051-2613e9d781d1"
      }, {
        "attributes": {
          "column_names": ["x", "y"],
          "tags": [],
          "doc": null,
          "selected": [],
          "discrete_ranges": {},
          "cont_ranges": {},
          "data": {
            "y": [0.0, 0.2486898871648548, 0.4817536741017153, 0.6845471059286887, 0.8443279255020151, 0.9510565162951535, 0.9980267284282716, 0.9822872507286886, 0.9048270524660195, 0.7705132427757893, 0.5877852522924732, 0.3681245526846777, 0.1253332335643041, -0.1253332335643043, -0.36812455268467836, -0.5877852522924734, -0.7705132427757894, -0.9048270524660198, -0.9822872507286887, -0.9980267284282716, -0.9510565162951536, -0.8443279255020151, -0.6845471059286883, -0.4817536741017153, -0.2486898871648545, 6.432490598706546e-16, 0.2486898871648549, 0.4817536741017157, 0.6845471059286893, 0.8443279255020152, 0.9510565162951538, 0.9980267284282716, 0.9822872507286886, 0.9048270524660192, 0.7705132427757886, 0.5877852522924734, 0.368124552684678, 0.1253332335643039, -0.12533323356430495, -0.3681245526846789, -0.5877852522924728, -0.7705132427757891, -0.9048270524660197, -0.9822872507286888, -0.9980267284282714, -0.9510565162951532, -0.8443279255020152, -0.6845471059286885, -0.4817536741017147, -0.24868988716485388, 1.2864981197413093e-15, 0.24868988716485463, 0.4817536741017155, 0.6845471059286891, 0.8443279255020155, 0.951056516295154, 0.9980267284282717, 0.9822872507286886, 0.9048270524660194, 0.7705132427757887, 0.5877852522924722, 0.36812455268467653, 0.12533323356430415, -0.1253332335643047, -0.3681245526846787, -0.5877852522924726, -0.7705132427757901, -0.9048270524660196, -0.982287250728689, -0.9980267284282716, -0.9510565162951538, -0.8443279255020143, -0.6845471059286887, -0.48175367410171344, -0.2486898871648541, -7.347880794884119e-16, 0.24868988716485615, 0.4817536741017152, 0.6845471059286902, 0.8443279255020154, 0.9510565162951533, 0.9980267284282717, 0.9822872507286887, 0.9048270524660187, 0.7705132427757888, 0.5877852522924709, 0.36812455268467675, 0.1253332335643044, -0.1253332335643062, -0.3681245526846785, -0.5877852522924752, -0.77051324277579, -0.9048270524660195, -0.982287250728689, -0.9980267284282716, -0.9510565162951528, -0.8443279255020144, -0.6845471059286888, -0.48175367410171366, -0.24868988716485438],
            "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
          },
          "id": "67a87110-8495-44af-9d1d-e997e162785e"
        },
        "type": "ColumnDataSource",
        "id": "67a87110-8495-44af-9d1d-e997e162785e"
      }, {
        "attributes": {
          "x_range": {
            "type": "DataRange1d",
            "id": "a8246692-a92d-4230-b127-bc9d57a42e42"
          },
          "right": [],
          "data_sources": [],
          "y_range": {
            "type": "DataRange1d",
            "id": "e5ba3426-36b1-4237-9f0f-9011e6b71009"
          },
          "title": "Plot",
          "renderers": [{
            "type": "LinearAxis",
            "id": "1eae71bd-ecee-4eba-97fe-44a6320a67b1"
          }, {
            "type": "Grid",
            "id": "50b0fd6a-b321-4f17-96db-b11f1a388777"
          }, {
            "type": "LinearAxis",
            "id": "561a6a55-271e-4fe4-874c-5effbedc6b4c"
          }, {
            "type": "Grid",
            "id": "19556a6b-49d8-47da-bd24-196946796cf9"
          }, {
            "type": "BoxSelection",
            "id": "19843ff5-eaa0-44bd-b279-18a96222f129"
          }, {
            "type": "BoxSelection",
            "id": "37457aff-27fc-48f1-b051-2613e9d781d1"
          }, {
            "type": "Glyph",
            "id": "557135f0-56dd-42d9-9876-064705c89db1"
          }],
          "extra_y_ranges": {},
          "extra_x_ranges": {},
          "above": [],
          "doc": null,
          "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67",
          "tools": [{
            "type": "PanTool",
            "id": "5c40787e-fda8-4e8e-b805-9704f05ae95a"
          }, {
            "type": "WheelZoomTool",
            "id": "5508a242-72a2-4dc9-8fc9-73b753d68891"
          }, {
            "type": "BoxZoomTool",
            "id": "f95a4be0-90b6-4409-a94f-e37f5b515969"
          }, {
            "type": "PreviewSaveTool",
            "id": "100d6675-7326-4b78-a8a9-87ff4a494915"
          }, {
            "type": "ResizeTool",
            "id": "bee75dca-8c70-4e93-a8a2-91e453f93c7a"
          }, {
            "type": "BoxSelectTool",
            "id": "b8a71ddd-6f4f-414a-a5dc-63fec650e9c8"
          }, {
            "type": "ResetTool",
            "id": "63229469-2a4a-4fce-8da5-adb0aa9705e4"
          }],
          "below": [{
            "type": "LinearAxis",
            "id": "1eae71bd-ecee-4eba-97fe-44a6320a67b1"
          }],
          "tags": [],
          "left": [{
            "type": "LinearAxis",
            "id": "561a6a55-271e-4fe4-874c-5effbedc6b4c"
          }]
        },
        "type": "Plot",
        "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
      }, {
        "attributes": {
          "sources": [{
            "source": {
              "type": "ColumnDataSource",
              "id": "67a87110-8495-44af-9d1d-e997e162785e"
            },
            "columns": ["x"]
          }],
          "tags": [],
          "doc": null,
          "id": "a8246692-a92d-4230-b127-bc9d57a42e42"
        },
        "type": "DataRange1d",
        "id": "a8246692-a92d-4230-b127-bc9d57a42e42"
      }, {
        "attributes": {
          "sources": [{
            "source": {
              "type": "ColumnDataSource",
              "id": "67a87110-8495-44af-9d1d-e997e162785e"
            },
            "columns": ["y"]
          }],
          "tags": [],
          "doc": null,
          "id": "e5ba3426-36b1-4237-9f0f-9011e6b71009"
        },
        "type": "DataRange1d",
        "id": "e5ba3426-36b1-4237-9f0f-9011e6b71009"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "formatter": {
            "type": "BasicTickFormatter",
            "id": "40e26c77-f2be-451c-8f7e-accf9aea2c7f"
          },
          "ticker": {
            "type": "BasicTicker",
            "id": "ed7bb138-3115-4030-9c3e-7c14da041f93"
          },
          "id": "1eae71bd-ecee-4eba-97fe-44a6320a67b1"
        },
        "type": "LinearAxis",
        "id": "1eae71bd-ecee-4eba-97fe-44a6320a67b1"
      }, {
        "attributes": {
          "tags": [],
          "doc": null,
          "id": "ed7bb138-3115-4030-9c3e-7c14da041f93",
          "num_minor_ticks": 5
        },
        "type": "BasicTicker",
        "id": "ed7bb138-3115-4030-9c3e-7c14da041f93"
      }, {
        "attributes": {
          "doc": null,
          "id": "40e26c77-f2be-451c-8f7e-accf9aea2c7f",
          "tags": []
        },
        "type": "BasicTickFormatter",
        "id": "40e26c77-f2be-451c-8f7e-accf9aea2c7f"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "dimension": 0,
          "ticker": {
            "type": "BasicTicker",
            "id": "ed7bb138-3115-4030-9c3e-7c14da041f93"
          },
          "id": "50b0fd6a-b321-4f17-96db-b11f1a388777"
        },
        "type": "Grid",
        "id": "50b0fd6a-b321-4f17-96db-b11f1a388777"
      }, {
        "attributes": {
          "tags": [],
          "doc": null,
          "tool": {
            "type": "BoxZoomTool",
            "id": "f95a4be0-90b6-4409-a94f-e37f5b515969"
          },
          "id": "19843ff5-eaa0-44bd-b279-18a96222f129"
        },
        "type": "BoxSelection",
        "id": "19843ff5-eaa0-44bd-b279-18a96222f129"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "formatter": {
            "type": "BasicTickFormatter",
            "id": "5bc4a678-92d0-4dce-a4d1-c2f10d57b0d3"
          },
          "ticker": {
            "type": "BasicTicker",
            "id": "199cac27-1181-4f6e-8e82-852c49363234"
          },
          "id": "561a6a55-271e-4fe4-874c-5effbedc6b4c"
        },
        "type": "LinearAxis",
        "id": "561a6a55-271e-4fe4-874c-5effbedc6b4c"
      }, {
        "attributes": {
          "tags": [],
          "doc": null,
          "id": "199cac27-1181-4f6e-8e82-852c49363234",
          "num_minor_ticks": 5
        },
        "type": "BasicTicker",
        "id": "199cac27-1181-4f6e-8e82-852c49363234"
      }, {
        "attributes": {
          "doc": null,
          "id": "5bc4a678-92d0-4dce-a4d1-c2f10d57b0d3",
          "tags": []
        },
        "type": "BasicTickFormatter",
        "id": "5bc4a678-92d0-4dce-a4d1-c2f10d57b0d3"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "dimension": 1,
          "ticker": {
            "type": "BasicTicker",
            "id": "199cac27-1181-4f6e-8e82-852c49363234"
          },
          "id": "19556a6b-49d8-47da-bd24-196946796cf9"
        },
        "type": "Grid",
        "id": "19556a6b-49d8-47da-bd24-196946796cf9"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "dimensions": ["width", "height"],
          "tags": [],
          "doc": null,
          "id": "5c40787e-fda8-4e8e-b805-9704f05ae95a"
        },
        "type": "PanTool",
        "id": "5c40787e-fda8-4e8e-b805-9704f05ae95a"
      }, {
        "attributes": {
          "x_range_name": "default",
          "data_source": {
            "type": "ColumnDataSource",
            "id": "67a87110-8495-44af-9d1d-e997e162785e"
          },
          "name": null,
          "server_data_source": null,
          "doc": null,
          "nonselection_glyphspec": {
            "line_color": {
              "value": "#1f77b4"
            },
            "line_alpha": {
              "units": "data",
              "value": 0.1
            },
            "angle_units": "deg",
            "length_units": "screen",
            "type": "line",
            "y": {
              "units": "data",
              "field": "y"
            },
            "line_width": {
              "units": "data",
              "field": "line_width"
            },
            "start_angle_units": "deg",
            "valign": null,
            "radius_units": "screen",
            "visible": null,
            "x": {
              "units": "data",
              "field": "x"
            },
            "end_angle_units": "deg",
            "line_dash_offset": 0,
            "line_cap": "butt",
            "line_dash": [],
            "margin": null,
            "line_join": "miter",
            "halign": null
          },
          "y_range_name": "default",
          "glyphspec": {
            "line_color": {
              "value": "red"
            },
            "line_width": {
              "units": "data",
              "field": "line_width"
            },
            "line_alpha": {
              "units": "data",
              "value": 1.0
            },
            "y": {
              "units": "data",
              "field": "y"
            },
            "x": {
              "units": "data",
              "field": "x"
            },
            "type": "line"
          },
          "id": "557135f0-56dd-42d9-9876-064705c89db1"
        },
        "type": "Glyph",
        "id": "557135f0-56dd-42d9-9876-064705c89db1"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "id": "100d6675-7326-4b78-a8a9-87ff4a494915"
        },
        "type": "PreviewSaveTool",
        "id": "100d6675-7326-4b78-a8a9-87ff4a494915"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "dimensions": ["width", "height"],
          "tags": [],
          "doc": null,
          "id": "5508a242-72a2-4dc9-8fc9-73b753d68891"
        },
        "type": "WheelZoomTool",
        "id": "5508a242-72a2-4dc9-8fc9-73b753d68891"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "id": "f95a4be0-90b6-4409-a94f-e37f5b515969"
        },
        "type": "BoxZoomTool",
        "id": "f95a4be0-90b6-4409-a94f-e37f5b515969"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "id": "63229469-2a4a-4fce-8da5-adb0aa9705e4"
        },
        "type": "ResetTool",
        "id": "63229469-2a4a-4fce-8da5-adb0aa9705e4"
      }, {
        "attributes": {
          "plot": null,
          "tags": [],
          "doc": null,
          "id": "b8a71ddd-6f4f-414a-a5dc-63fec650e9c8",
          "renderers": [{
            "type": "Glyph",
            "id": "557135f0-56dd-42d9-9876-064705c89db1"
          }]
        },
        "type": "BoxSelectTool",
        "id": "b8a71ddd-6f4f-414a-a5dc-63fec650e9c8"
      }, {
        "attributes": {
          "plot": {
            "type": "Plot",
            "id": "d3931b04-ba1d-4a7b-bcd6-9dc0c1175f67"
          },
          "tags": [],
          "doc": null,
          "id": "bee75dca-8c70-4e93-a8a2-91e453f93c7a"
        },
        "type": "ResizeTool",
        "id": "bee75dca-8c70-4e93-a8a2-91e453f93c7a"
      }];
      Bokeh.load_models(all_models);
      var model = Bokeh.Collections(modeltype).get(modelid);
      var view = new model.default_view({
        model: model,
        el: '#68e7fcd1-5f9c-4ac7-990b-09a0a56790fc'
      });
    });
  </script>
</head>

<body>
  <div class="plotdiv" id="68e7fcd1-5f9c-4ac7-990b-09a0a56790fc"></div>
</body>

</html>

于 2015-03-23T13:26:18.193 回答