0

I have made some graphs with matplotlib with fitted curves. Usually, to print out the resulting parameters of these curve fits I put them in the label of your plotted curve like so: plt.plot(x,yfit, label=fr”{popt[0]:.2f} $\pm$ {pcov[0]:.2f}”)

This prints the values very nicely inside the legend of the graph, and allows me to take advantage of LaTeX formatting and include symbols such as the plus/minus sign.

However, the curves I am fitting take up to 6 parameters and since I have several curves in the same graph, I just don’t have the space to print out all the results like this. What I would like is basically a method/python package that allows me to print the values as a table, with the option to format it like I would a LaTeX table. Ideally, it would be a package that allows me to create a LaTeX table, put then accept the input values similar to what I do when I call fr'{popt[0]}', so it would look something like this:

Example of parameter table

This is different from using the tabular option, because I am not looking to just print a table that looks like python text. I want it to output as a LaTeX table (or within a .png image) whose formatting options I can have control over (namely so I can write in the headers, choose which values I place in each cell of the table, and insert LaTeX symbols).

I don't know if there is already a built in matplotlib option that has this, but I have so far searched and not found any.

Rye
  • 167
  • 8
  • Of course there is a method to do this, you can simply print each row one after another. What was the difficulty about this? – mkrieger1 Jun 30 '23 at 12:06
  • Printing just the values does not give me the formatting I want, taking advantage of writing in LaTeX symbols, like I explained, which is something that you can do when defining the label. What I want is to write the values into a table so they are organized. – Rye Jun 30 '23 at 12:07
  • What does not print in the right formatting? What is not right about `print(" 0.1 | 0.43509 | 8.5293 ")` etc.? You can print `±` if you want. – mkrieger1 Jun 30 '23 at 12:07
  • Does this answer your question? [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) – mkrieger1 Jun 30 '23 at 12:11
  • 1
    Not really, what I am asking is basically if there is a way to write tables with python and format them to look the way they do when you make them in LaTeX but by inputting the values I calculated – Rye Jun 30 '23 at 12:12
  • 1
    [Matplotlib KNOWS how to draw a table](https://matplotlib.org/stable/gallery/misc/table_demo.html#sphx-glr-gallery-misc-table-demo-py). – gboffi Jun 30 '23 at 12:12
  • Another idea, if you are comfortable with LaTeX, you can make your Python code ① fit the curves, ② plot the curves, ③ save the Figure to PDF, ④ write to a file the LaTeX code (using the 'standalone' package) that includes the image and generates the table, ⑤ run LaTeX, convert to whatever, clean up. – gboffi Jun 30 '23 at 12:28
  • 1
    Check this out. [https://www.overleaf.com/learn/latex/Tables](https://www.overleaf.com/learn/latex/Tables). It seems like you could try to make python code that prints the latex syntax, and then you could feed it into an interpreter or something – vs07 Jun 30 '23 at 12:35
  • @gboffi I think your link is exactly what I wanted to! Didn't know matplotlib had a table drawing option, thanks – Rye Jun 30 '23 at 12:57
  • 2
    Matplotlib does have the kitchen sink too. ;-) – gboffi Jun 30 '23 at 13:00

0 Answers0