{"id":16489827,"url":"https://github.com/wookayin/tensorflow-plot","last_synced_at":"2025-04-05T03:04:33.208Z","repository":{"id":57474155,"uuid":"86198950","full_name":"wookayin/tensorflow-plot","owner":"wookayin","description":"📈 TensorFlow + Matplotlib as TF ops","archived":false,"fork":false,"pushed_at":"2019-07-24T04:45:53.000Z","size":5045,"stargazers_count":298,"open_issues_count":5,"forks_count":43,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-12T13:45:19.642Z","etag":null,"topics":["matplotlib","plot","python","tensorboard","tensorflow","tfplot"],"latest_commit_sha":null,"homepage":"http://tensorflow-plot.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wookayin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-26T01:11:48.000Z","updated_at":"2024-01-04T16:12:39.000Z","dependencies_parsed_at":"2022-09-12T19:40:19.102Z","dependency_job_id":null,"html_url":"https://github.com/wookayin/tensorflow-plot","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wookayin%2Ftensorflow-plot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wookayin%2Ftensorflow-plot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wookayin%2Ftensorflow-plot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wookayin%2Ftensorflow-plot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wookayin","download_url":"https://codeload.github.com/wookayin/tensorflow-plot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280262,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matplotlib","plot","python","tensorboard","tensorflow","tfplot"],"created_at":"2024-10-11T13:45:33.067Z","updated_at":"2025-04-05T03:04:33.189Z","avatar_url":"https://github.com/wookayin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"TensorFlow Plot (tfplot)\n========================\n\n[![pypi](https://img.shields.io/pypi/v/tensorflow-plot.svg?maxAge=86400)][pypi_tfplot]\n[![Documentation Status](https://readthedocs.org/projects/tensorflow-plot/badge/?version=latest)][documentation]\n[![Build Status](https://travis-ci.org/wookayin/tensorflow-plot.svg?branch=master)](https://travis-ci.org/wookayin/tensorflow-plot)\n\nA [TensorFlow][tensorflow] utility for providing matplotlib-based **plot** operations\n— [TensorBoard][tensorboard] ❤️ [Matplotlib][matplotlib].\n\n\u003cp align=\"center\"\u003e\n\u003ci\u003e 🚧 Under Construction —  API might change!\u003c/i\u003e\n\u003c/p\u003e\n\nIt allows us to draw **_any_** [matplotlib][matplotlib] plots or figures into images,\nas a part of TensorFlow computation graph.\nEspecially, we can easily any plot and see the result image\nas an image summary in [TensorBoard][tensorboard].\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"./assets/tensorboard-plot-summary.png\" width=\"70%\" /\u003e\n\u003c/p\u003e\n\n\nQuick Overview\n--------------\n\nThere are two main ways of using `tfplot`: (i) Use as TF op, and (ii) Manually add summary protos.\n\n### Usage: Decorator\n\nYou can directly declare a Tensor factory by using [`tfplot.autowrap`][tfplot-autowrap] as a decorator.\nIn the body of the wrapped function you can add any logic for drawing plots. Example:\n\n```python\n@tfplot.autowrap(figsize=(2, 2))\ndef plot_scatter(x: np.ndarray, y: np.ndarray, *, ax, color='red'):\n    ax.scatter(x, y, color=color)\n\nx = tf.constant([1, 2, 3], dtype=tf.float32)     # tf.Tensor\ny = tf.constant([1, 4, 9], dtype=tf.float32)     # tf.Tensor\nplot_op = plot_scatter(x, y)                     # tf.Tensor shape=(?, ?, 4) dtype=uint8\n```\n\n\n### Usage: Wrap as TF ops\n\nWe can [wrap][tfplot-autowrap] **any** pure python function for plotting as a Tensorflow op, such as:\n\n- (i) A python function that creates and return a matplotlib `Figure` (see below)\n- (ii) A python function that has `fig` or `ax` keyword parameters (will be auto-injected);\n  e.g. [`seaborn.heatmap`](http://seaborn.pydata.org/generated/seaborn.heatmap.html)\n- (iii) A method instance of [matplotlib `Axes`](https://matplotlib.org/api/axes_api.html);\n  e.g. [`Axes.scatter`](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter)\n\nExample of (i): You can define a python function that takes `numpy.ndarray` values as input (as an argument of Tensor input),\nand draw a plot as a return value of `matplotlib.figure.Figure`.\nThe resulting TensorFlow plot op will be a RGBA image tensor of shape `[height, width, 4]` containing the resulting plot.\n\n\n```python\ndef figure_heatmap(heatmap, cmap='jet'):\n    # draw a heatmap with a colorbar\n    fig, ax = tfplot.subplots(figsize=(4, 3))       # DON'T USE plt.subplots() !!!!\n    im = ax.imshow(heatmap, cmap=cmap)\n    fig.colorbar(im)\n    return fig\n\nheatmap_tensor = ...   # tf.Tensor shape=(16, 16) dtype=float32\n\n# (a) wrap function as a Tensor factory\nplot_op = tfplot.autowrap(figure_heatmap)(heatmap_tensor)      # tf.Tensor shape=(?, ?, 4) dtype=uint8\n\n# (b) direct invocation similar to tf.py_func\nplot_op = tfplot.plot(figure_heatmap, [heatmap_tensor], cmap='jet')\n\n# (c) or just directly add an image summary with the plot\ntfplot.summary.plot(\"heatmap_summary\", figure_heatmap, [heatmap_tensor])\n```\n\nExample of (ii):\n\n```python tfplot\nimport tfplot\nimport seaborn.apionly as sns\n\ntf_heatmap = tfplot.autowrap(sns.heatmap, figsize=(4, 4), batch=True)   # function: Tensor -\u003e Tensor\nplot_op = tf_heatmap(attention_maps)   # tf.Tensor shape=(?, 400, 400, 4) dtype=uint8\ntf.summary.image(\"attention_maps\", plot_op)\n```\n\nPlease take a look at the [the showcase][examples-showcase] or [examples directory][examples-dir] for more examples and use cases.\n\n[The full documentation][documentation] including API docs can be found at [readthedocs][documentation].\n\n\n### Usage: Manually add summary protos\n\n```python\nimport tensorboard as tb\nfig, ax = ...\n\n# Get RGB image manually or by executing plot ops.\nembedding_plot = sess.run(plot_op)                 # ndarray [H, W, 3] uint8\nembedding_plot = tfplot.figure_to_array(fig)       # ndarray [H, W, 3] uint8\n\nsummary_pb = tb.summary.image_pb('plot_embedding', [embedding_plot])\nsummary_writer.write_add_summary(summary_pb, global_step=global_step)\n```\n\n\nInstallation\n------------\n\n```\npip install tensorflow-plot\n```\n\nTo grab the latest development version:\n\n```\npip install git+https://github.com/wookayin/tensorflow-plot.git@master\n```\n\nNote\n----\n\n### Some comments on Speed\n\n* Matplotlib operations can be **very** slow as Matplotlib runs in python rather than native code,\nso please watch out for runtime speed.\nThere is still a room for improvement, which will be addressed in the near future.\n\n* Moreover, it might be also a good idea to draw plots from the main code (rather than having a TF op) and add them as image summaries.\nPlease use this library at your best discernment.\n\n### Thread-safety issue\n\nPlease use **object-oriented** matplotlib APIs (e.g. `Figure`, `AxesSubplot`)\ninstead of [pyplot] APIs (i.e. `matplotlib.pyplot` or `plt.XXX()`)\nwhen creating and drawing plots.\nThis is because [pyplot] APIs are not *thread-safe*,\nwhile the TensorFlow plot operations are usually executed in multi-threaded manners.\n\nFor example, avoid any use of `pyplot` (or `plt`):\n\n```python\n# DON'T DO LIKE THIS !!!\ndef figure_heatmap(heatmap):\n    fig = plt.figure()                 # \u003c--- NO!\n    plt.imshow(heatmap)\n    return fig\n```\n\nand do it like:\n\n```python\ndef figure_heatmap(heatmap):\n    fig = matplotlib.figure.Figure()   # or just `fig = tfplot.Figure()`\n    ax = fig.add_subplot(1, 1, 1)      # ax: AxesSubplot\n    # or, just `fig, ax = tfplot.subplots()`\n    ax.imshow(heatmap)\n    return fig                         # fig: Figure\n```\n\nFor example, `tfplot.subplots()` is a good replacement for `plt.subplots()`\nto use inside plot functions.\nAlternatively, you can just take advantage of automatic injection of `fig` and/or `ax`.\n\n\n[pypi_tfplot]: https://pypi.python.org/pypi/tensorflow-plot\n[matplotlib]: http://matplotlib.org/\n[tensorflow]: https://www.tensorflow.org/\n[tensorboard]: https://www.tensorflow.org/get_started/summaries_and_tensorboard\n[pyplot]: http://matplotlib.org/api/pyplot_api.html\n[examples-dir]: https://github.com/wookayin/tensorflow-plot/blob/master/examples/\n[examples-showcase]: https://github.com/wookayin/tensorflow-plot/blob/master/examples/showcases.ipynb\n[documentation]: http://tensorflow-plot.readthedocs.io/en/latest/\n\n[tfplot-autowrap]: https://tensorflow-plot.readthedocs.io/en/latest/api/tfplot.html#tfplot.autowrap\n\n\n### TensorFlow compatibility\n\nCurrently, `tfplot` is compatible with TensorFlow 1.x series.\nSupport for eager execution and TF 2.0 will be coming soon!\n\n\nLicense\n-------\n\n[MIT License](LICENSE) © Jongwook Choi\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwookayin%2Ftensorflow-plot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwookayin%2Ftensorflow-plot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwookayin%2Ftensorflow-plot/lists"}