{"id":16087150,"url":"https://github.com/juliendelplanque/matplotlibbridge","last_synced_at":"2025-03-18T06:30:45.474Z","repository":{"id":83914494,"uuid":"75126087","full_name":"juliendelplanque/MatplotLibBridge","owner":"juliendelplanque","description":"A bridge to provide the ability to Pharo user to use Python's Matplotlib.","archived":false,"fork":false,"pushed_at":"2019-05-20T07:56:04.000Z","size":599,"stargazers_count":19,"open_issues_count":3,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-28T07:49:32.656Z","etag":null,"topics":["matplotlib","pharo","python","smalltalk"],"latest_commit_sha":null,"homepage":null,"language":"Smalltalk","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/juliendelplanque.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-11-29T22:01:12.000Z","updated_at":"2023-11-24T08:27:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"56c67af5-3f13-472a-8aec-855a689864fd","html_url":"https://github.com/juliendelplanque/MatplotLibBridge","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliendelplanque%2FMatplotLibBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliendelplanque%2FMatplotLibBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliendelplanque%2FMatplotLibBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliendelplanque%2FMatplotLibBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliendelplanque","download_url":"https://codeload.github.com/juliendelplanque/MatplotLibBridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243907015,"owners_count":20367141,"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","pharo","python","smalltalk"],"created_at":"2024-10-09T13:28:15.333Z","updated_at":"2025-03-18T06:30:44.665Z","avatar_url":"https://github.com/juliendelplanque.png","language":"Smalltalk","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MatplotLibBridge\nA bridge to provide the ability to Pharo user to use Python's Matplotlib.\n\n## Install\nThis project has been tested on Linux and Mac OS. If someone manage to use it on Windows, please let me know.\n### Python side\nObviously, you need to have Python 3 installed on your machine. Furthermore, you need matplotlib package installed as well. The following link explains how to do it [install matplotlib](https://matplotlib.org/faq/installing_faq.html).\n\n### Pharo side\nTo install this project in your image, open a playground and execute the following code snippet:\n```\nMetacello new\n    baseline: 'MatplotLibBridge';\n    repository: 'github://juliendelplanque/MatplotLibBridge/src';\n    load\n```\n\nThis will install version 0.1 of the project. You should only depend on released versions for your projects. The master branch of this repository contains latest commits and might not be stable.\n\nOnce MatplotLibBridge is installed in your image, you need to configure [Python3Generator](https://github.com/juliendelplanque/Python3Generator) to use the Python interpreter you prefer. My advise is to use the FFI interpreter using: `P3GInterpreter useFFIInterpreter`. If your `python3` binary is located in a standard path in your file system, it should be fine, else you can manually set the path to `python3` binary using for example: `P3GInterpreter current pathToPython: '/usr/bin/python3'`.\n\n## How to use MatplotLibBridge?\nThis page provides some examples to illustrate how to use MatplotLibBridge. For each example, the method `#show` is called when the plot is built. This method open an interactive window showing a preview of the plot. If you want to export the plot in a file, you can use `#generateIn:` which takes a `FileReference` as argument. This method creates an image file with a format corresponding to the extension of the file reference. The available formats are `.png`, `.svg` and `.pdf`. For example: `plot generateIn: '/tmp/foo.png'` will generate the `plot` in the file `/tmp/foo.png` in png format.\n\n### Creating a line plot\nThe following code snippet creates a line plot for the factorial function between 0 and 10. It also illustrates that it is possible to use LaTeX code as marker for the points. Using `#label:` message allows to give name to the line that will be displayed in a corner of the plot.\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 10) collect: [ :i | i @ i factorial ]);\n\t\t\tmarker: '$\\triangledown$';\n\t\t\tlabel: 'Factorial' ];\n\tshow\n```\n\n![exampleLineplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleLineplot.png)\n\n### Creating a line plot using Y-Block line\nThe following code snippet creates a line plot for the factorial function between 0 and 10 using MLBYBlockLine. It is totally equivalent to the preceding example (in the result) but this is another way to express the plot using MatplotLibBridge.\n```\nMLBLinePlot new\n\taddYBlockLine: [ :line | \n\t\tline\n\t\t\tx: (0 to: 10);\n\t\t\tyBlock: #factorial;\n\t\t\tmarker: '$\\triangledown$';\n\t\t\tlabel: 'Factorial' ];\n\tshow\n```\n\n![exampleLineplotYBlockLine](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleLineplotYBlockLine.png)\n\n### Creating multi-lines plot\nThe following code snippet creates a multi-lines plot for the square root function, the third-root function and the fourth-root function between 0 and 1000. It also shows how to change the color of a line (using `#color:`) and how to change its style (using `#style:`). Available styles for a line can be accessed using `MLBLine\u003e\u003e#lineStyles`\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | line points: ((0 to: 1000) collect: [ :i | i @ i sqrt ]) ];\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 1000) collect: [ :i | i @ (i nthRoot: 3) ]);\n\t\t\tcolor: Color red;\n\t\t\tstyle: '--' ];\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 1000) collect: [ :i | i @ (i nthRoot: 4) ]);\n\t\t\tcolor: Color blue;\n\t\t\tstyle: 'dotted' ];\n\tshow\n```\n\n![exampleMultilineplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleMultilineplot.png)\n\n### Changing line width in line plot or multi-lines plot\nIt is possible to configure the width of each line in a line plot using `#width:`.\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 10) collect: [ :i | i @ i ]);\n\t\t\twidth: 5;\n\t\t\tcolor: Color blue ];\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 10) collect: [ :i | i @ (i * 2) ]);\n\t\t\twidth: 3;\n\t\t\tcolor: Color red ];\n\taddPointsLine: [ :line | \n\t\t\"#width = 1 if not specified.\"\n\t\tline\n\t\t\tpoints: ((0 to: 10) collect: [ :i | i @ (i * 3) ]);\n\t\t\tcolor: Color green ];\n\tshow\n```\n\n![exampleChangingLineWidthOfLineplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleChangingLineWidthOfLineplot.png)\n\n### Discontinuous lines in line plot or multi-lines plot\nIf you create points having `Float nan` as x or y, it allows to create discontinuous lines.\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints:\n\t\t\t\t((-10 to: -0) collect: [ :i | i @ 1 negated ]) , {(0 @ Float nan)}\n\t\t\t\t\t, ((0 to: 10) collect: [ :i | i @ 1 ]);\n\t\t\tlabel: 'Discontinous line';\n\t\t\tcolor: Color red ];\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints:\n\t\t\t\t((-10 to: -0) collect: [ :i | i @ 0.5 negated ])\n\t\t\t\t\t, ((0 to: 10) collect: [ :i | i @ 0.5 ]);\n\t\t\tlabel: 'Continous line';\n\t\t\tcolor: Color blue ];\n\tshow\n```\n\n![exampleDiscontinuousLineplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleDiscontinuousLineplot.png)\n\n### Set an axis to be in logarithmic scale\nCalling the method `#logScale` in #configYAxis block makes the scale of the concerned axis logarithmic. Other scales are available, see `MLBAbstractAxis\u003e\u003evalidScales`.\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((0 to: 100) collect: [ :i | i @ i exp ]);\n\t\t\tlabel: 'exp(x)' ];\n\tconfigYAxis: [ :yAxis |\n\t\tyAxis logScale ];\n\tshow\n```\n\n![exampleLogscaleLineplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleLogscaleLineplot.png)\n\n### Creating a bar plot\nThe following code snippet creates a bar plot for some data and to display their standard deviation. This example also illustrates how to do some basic configuration of x and y axes (`#configXAxis:` and `#configYAxis:`).\n```\n|data std |\ndata := #(20 35 30 35 27).\nstd := #(2 3 4 1 2).\n\nMLBBarPlot new\n\tdata: data;\n\tlabels: #('G1' 'G2' 'G3' 'G4' 'G5');\n\tcolor: Color blue;\n\tconfigXAxis: [ :xAxis |\n\t\txAxis\n\t\t\tlabelsRotation: 45 ];\n\tconfigYAxis: [ :yAxis |\n\t\tyAxis\n\t\t\ttitle: 'Y label' ];\n\ttitle: 'Barplot example';\n\terrorBars: std;\n\terrorBarsColor: Color black;\n\talignLabelCenter;\n\tshow.\n```\n\n![exampleBarplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleBarplot.png)\n\n### Creating an horizontal box plot\nThe following code snippet creates an horizontal box plot for some random data.\n```\nMLBBoxPlot new\n\tdataList: {((1 to: 100) collect: [ :i | (1 to: 10) atRandom ]) . ((1 to: 100) collect: [ :i | (1 to: 10) atRandom ])};\n\tbeHorizontal;\n\tconfigYAxis: [ :axis|\n\t\taxis\n\t\t\tlabels: #('Data 1' 'Data 2') ];\n\tshow\n```\n\n![exampleHorizontalBoxplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleHorizontalBoxplot.png)\n\n### Creating a stacked bar plot\nThe following code snippet creates a stacked bar plot for some data and to display their standard deviation. The `#dataList` of a `MLBStackedBarPlot` should be a collection of collections of the same arity. It also shows how to rotate the labels of an axis.\n```\n| menMeans womenMeans menStd womenStd data std |\nmenMeans := #(20 35 30 35 27).\nwomenMeans := #(25 32 34 20 25).\nmenStd := #(2 3 4 1 2).\nwomenStd := #(3 5 2 3 3).\n\ndata := (1 to: menMeans size) collect: [ :i |\n\t\t\t{ menMeans at: i. womenMeans at: i } ].\nstd := (1 to: menStd size) collect: [ :i |\n\t\t\t{ menStd at: i. womenStd at: i } ].\n\nMLBStackedBarPlot new\n\tdataList: data;\n\tcolorList: {Color blue. Color pink};\n\ttitle: 'Scores by group and gender';\n\tconfigXAxis: [ :xAxis |\n\t\txAxis\n\t\t\tlabelsRotation: 45;\n\t\t\tlabels: #('G1' 'G2' 'G3' 'G4' 'G5') ];\n\tconfigYAxis: [ :yAxis |\n\t\tyAxis\n\t\t\ttitle: 'Scores' ];\n\terrorBarsList: std;\n\terrorBarsColorList: { Color black . Color black };\n\talignLabelCenter;\n\tshow.\n```\n\n![exampleStackedBarplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleStackedBarplot.png)\n\n### Creating a vertical box plot\nThe following code snippet creates a vertical box plot for some random data. No need to call `#beVertical` since this is the default orientation of the plot.\n```\nMLBBoxPlot new\n\tdataList: {((1 to: 100) collect: [ :i | (1 to: 10) atRandom ])};\n\tconfigXAxis: [ :xAxis|\n\t\txAxis\n\t\t\tlabels: #('Data') ];\n\tshow\n```\n\n![exampleVerticalBoxplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleVerticalBoxplot.png)\n\n### Creating a pie plot\nThe following code snippet creates a pie box plot for some data that sum to 100. You can optionally add `labels:`, set the a shadow or not (`hasShadow:`), configure the `axis:` (here we want to have a round pie, so we set it to `'equal'`), explode each part of the pie at different degrees (here only the part concerning `30` data is exploded) and choose the start angle.\n```\nMLBPiePlot new\n\tdata: #(50 20 30);\n\tlabels: #(One Two Three);\n\thasShadow: false;\n\taxis: 'equal';\n\texplode: #(0 0 0.1);\n\tstartAngle: -90;\n\tshow\n```\n\n![examplePieplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/examplePieplot.png)\n\n### Creating a scatter plot (new API)\nThe following code snippet creates a scatter plot for some random data. The principle is to create MLBScatterData, to set them a position, a color, a size and a shape\n```\nMLBScatterPlot2 new\n\tdata: ((1 to: 20) collect: [ :i | \n\t\t(MLBScatterData position: (1 to: 15) atRandom @ (1 to: 15) atRandom size: (20 to: 500) atRandom)\n\t\t\tcolor: Color random;\n\t\t\tmarker: MLBConstants markers atRandom;\n\t\t\tyourself ]);\n\tshow\n```\n\n![exampleScatterplot2](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleScatterplot2.png)\n\n### Creating a scatter plot (old API)\nThe following code snippet creates a scatter plot for some random data. Here we create `MLBCircle`s with a random position, a random diameter and a random color and provide them to the `MLBScatterPlot` instance.\n```\nMLBScatterPlot new\n\tcircles: ((1 to: 10) collect: [ :i | \n\t\t(MLBCircle position: (1 to: 15) atRandom @ (1 to: 15) atRandom size: (20 to: 500) atRandom)\n\t\t\tcolor: Color random;\n\t\t\tyourself ]);\n\tshow\n```\n\n![exampleScatterplot](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleScatterplot.png)\n\n### Using LaTeX in legend\nThe following code snippet shows you how to create a line plot with LaTeX code for lines' labels. In fact, with MatplotLibBridge, you can use LaTeX in any String you provide.\n```\n| interval |\ninterval := 1 to: 1000.\nMLBLinePlot new\n\taddPointsLine: [ :line |\n\t\tline\n\t\t\tpoints: (interval collect: [ :i | i @ i sqrt ]);\n\t\t\tcolor: Color green;\n\t\t\tlabel: '$\\sqrt[2]{x}$' ];\n\taddPointsLine: [ :line |\n\t\tline\n\t\t\tpoints: (interval collect: [ :i | i @ (i nthRoot: 3) ]);\n\t\t\tcolor: Color red;\n\t\t\tlabel: '$\\sqrt[3]{x}$' ];\n\taddPointsLine: [ :line |\n\t\tline\n\t\t\tpoints: (interval collect: [ :i | i @ (i nthRoot: 4) ]);\n\t\t\tcolor: Color blue;\n\t\t\tlabel: '$\\sqrt[4]{x}$' ];\n\taddPointsLine: [ :line |\n\t\tline\n\t\t\tpoints: (interval collect: [ :i | i @ i log ]);\n\t\t\tlabel: '$log(x)$' ];\n\tconfigXAxis: [ :axis | axis min: 0 ];\n\tconfigYAxis: [ :axis | axis min: 0 ];\n\taddLegend;\n\tshow\n```\n\n![exampleLegend](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleLegend.png)\n\n### Annotations\nOn any plot, you can add annotations. An annotation is an extra graphical element you add on your plot such as for example an arrow pointing to a certain interesting location on the plot.\n```\nMLBLinePlot new\n\taddPointsLine: [ :line | \n\t\tline\n\t\t\tpoints: ((1 to: 50 by: 2) collect: [ :i | i @ (i ** 3) ]);\n\t\t\tstyle: 'solid';\n\t\t\tmarker: 'None';\n\t\t\tcolor:\n\t\t\t\t(Color\n\t\t\t\t\tr: 0\n\t\t\t\t\tg: 0\n\t\t\t\t\tb: 0\n\t\t\t\t\talpha: 0.3) ];\n\taddAnnotation: [ :annotation | \n\t\tannotation\n\t\t\tcontent: 'a thing';\n\t\t\tposition: 10 @ (10 ** 3);\n\t\t\ttextPosition: 10 @ (10 ** 4);\n\t\t\tarrowProperties: {('arrowstyle' -\u003e '\u003c|-')} asDictionary ];\n\taddAnnotation: [ :annotation | \n\t\tannotation\n\t\t\tcontent: 'another thing';\n\t\t\tposition: 30 @ (30 ** 3);\n\t\t\ttextPosition: 35 @ (30 ** 3 + 10);\n\t\t\tarrowProperties:\n\t\t\t\t{('facecolor' -\u003e 'black').\n\t\t\t\t('shrink' -\u003e 4)} asDictionary ];\n\tshow\n```\n\n![exampleAnnotations](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleAnnotations.png)\n\n### Style sheet\nMatplotLib allows to use stylesheet to reuse/change easily plot styles. The bridge actually expose this API with the `MLBStyleSheet` object. The name of properties are the same as in the python version. For more information see the official documentation of matplotlib: https://matplotlib.org/users/customizing.html.\n```\n| style |\nstyle := MLBStyleSheet new\n\t\t\t\tsetProperty: 'color' ofGroup: 'text' to: 'red';\n\t\t\t\tsetProperty: 'weight' ofGroup: 'font' to: 'bold';\n\t\t\t\tyourself.\n\t\t\t\t\nMLBBarPlot new\n\tdata: #(1 2 3);\n\tlabels: #('Group 1' 'Group 2' 'Group 3');\n\ttitle: 'A title';\n\tstyle: style;\n\tshow\n```\n\n![exampleStyleSheet](https://raw.githubusercontent.com/juliendelplanque/MatplotLibBridge/master/plots/exampleStyleSheet.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliendelplanque%2Fmatplotlibbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliendelplanque%2Fmatplotlibbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliendelplanque%2Fmatplotlibbridge/lists"}