- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 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
但想不出如何让它工作。
最佳答案
您可能希望有机会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>
关于python - 将 matplotlib 图导出到 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19006901/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!