micropaul
micropaul

Reputation: 305

Julia: Drawing of a table besides a plot

I have the following requirement and wondering if I could solve it with Julia: draw a SVG plot with up to 4 curves on it. Each curve has to be constructed out of 6 data points. The plot title and the legend should be positioned on the top right corner outside of the plot area.

So far so easy, I'm pretty certain it's solvable with one of the great Julia metapackages. But this one drives me crazy: In the bottom right corner outside of the plot area a table should be drawn. It should contain the values of 6 data points of each curve.

Example:

|     pressure   |  1  |  2  |  3  |  4  |  5  |   6  |
|----------------|-----|-----|-----|-----|-----|------|
| colored line 1 | 3.8 | 3.9 | 4.1 | 5.0 | 9.1 | 10.0 |
| colored line 2 | 4.0 | 4.1 | 5.0 | 7.1 | 8.0 | 11.0 |
|      gal/min   |     |     |     |     |     |      |

The plot and the data table should be placed both inside the SVG. After studying the documentation for hours I'm still clueless how to draw this table. Is this even possible with a plotting library?

Upvotes: 2

Views: 710

Answers (2)

gboffi
gboffi

Reputation: 25093

image description? no

I don't speak Julia, I hope that you can translate my Python + matplotlib.pyplot into it.

The only tricky trick is the abnormal height ratio, but please try with a reasonable one, say (5, 1) to see how it comes out "wrong".

Of course the plot is not as detailed as specified by yours requirements, but I don't want to be offensive and fully complete your task.

import matplotlib.pyplot as plt
import numpy as np

data = np.array([tuple(map(float, line.split('|'))) for line in '''\
3.8 | 3.9 | 4.1 | 5.0 | 9.1 | 10.0
4.0 | 4.1 | 5.0 | 7.1 | 8.0 | 11.0
'''.splitlines()])

fig, (ax, tax) = plt.subplots(
    nrows=2, layout= 'constrained',
    gridspec_kw=dict(height_ratios=(29,1)))
tax.axis('off')
ax.plot(data.T)
tax.table(data)
plt.show()

Upvotes: 0

attacc
attacc

Reputation: 1

Here a simple example of a code that plot a table in Julia. It will generate the table using random numbers and DataFrames, and then pass it to PyPlot. In principle you can add color for each cell according to their value, but I'm a bit confused on how to do it.

using PyPlot
using Random
using DataFrames

# Generate random numbers
randn = Random.randn

# Create the index and the DataFrame
idx = 1:10
df = DataFrame(randn(10, 5), :auto)  # Create a DataFrame with 10 rows and 5 columns
rename!(df, [:A, :B, :C, :D, :E])    # Rename the columns to 'A', 'B', 'C', 'D', 'E'

# Round the values and compute normalization
vals = round.(Matrix(df), digits=2)  # Convert DataFrame to matrix and round values to 2 decimals

# Create the figure
fig, ax = subplots(figsize=(15, 8))  # Create a figure with specified size
ax[:set_frame_on](true)              # Enable the frame for the plot
ax[:set_xticks]([])                  # Remove x-axis ticks
ax[:set_yticks]([])                  # Remove y-axis ticks

# Create the table
the_table = ax[:table](
    cellText=vals,                   # Table cell values
    rowLabels=idx,                   # Row labels (index)
    colLabels=names(df),             # Column labels (DataFrame column names)
    colWidths=[0.03 for _ in 1:size(vals, 2)],  # Column width
    loc="center",                    # Center the table
)

# Display the figure
show()

Upvotes: 0

Related Questions