diff options
Diffstat (limited to '_posts/2023-08-01-make-b-w-svg-charts-with-matplotlib.md')
| -rw-r--r-- | _posts/2023-08-01-make-b-w-svg-charts-with-matplotlib.md | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/_posts/2023-08-01-make-b-w-svg-charts-with-matplotlib.md b/_posts/2023-08-01-make-b-w-svg-charts-with-matplotlib.md new file mode 100644 index 0000000..6e1fa14 --- /dev/null +++ b/_posts/2023-08-01-make-b-w-svg-charts-with-matplotlib.md | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | --- | ||
| 2 | title: "Make B/W SVG charts with matplotlib" | ||
| 3 | permalink: /make-b-w-svg-charts-with-matplotlib.html | ||
| 4 | date: 2023-08-01T17:04:10+02:00 | ||
| 5 | layout: post | ||
| 6 | type: note | ||
| 7 | draft: false | ||
| 8 | --- | ||
| 9 | |||
| 10 | Install pip requirements. | ||
| 11 | |||
| 12 | ```sh | ||
| 13 | pip install matplotlib | ||
| 14 | pip install pandas | ||
| 15 | ``` | ||
| 16 | |||
| 17 | Example of data being used. | ||
| 18 | |||
| 19 | ```text | ||
| 20 | Epoch,Connect (NLB),Processing (NLB),Waiting (NLB),Total (NLB),Connect (ALB),Processing (ALB),Waiting (ALB),Total (ALB) | ||
| 21 | 1,57.7,315.7,309.4,321.6,9,104.4,98.3,105.7 | ||
| 22 | 2,121.9,114.4,100.3,176.9,5.8,99.1,97.1,101.1 | ||
| 23 | 3,5.3,229.4,231.2,231.4,14.2,83,69.4,87.9 | ||
| 24 | 4,4.2,134.5,112.2,135.3,5.3,132.4,105.5,134.1 | ||
| 25 | 5,5.8,247.4,246.8,248.1,6,74.3,70.2,75.5 | ||
| 26 | 6,9.9,122.9,100.6,122.7,7.5,241.1,79.3,242.3 | ||
| 27 | 7,6.1,170.2,106.4,170.5,7.2,382.4,375.1,383.8 | ||
| 28 | 8,6.6,194.3,201.4,195.5,7.1,130.9,104.8,132.6 | ||
| 29 | 9,6.4,146.1,122.3,147.7,9.4,95.6,74,96.4 | ||
| 30 | ``` | ||
| 31 | |||
| 32 | In the code you can use `df` as dataframes and use the headers like `df["Epoch"]`. | ||
| 33 | This is how you get a column data with pandas. | ||
| 34 | |||
| 35 | The Python code responsible for generating a chart: | ||
| 36 | |||
| 37 | ```python | ||
| 38 | import csv | ||
| 39 | import sys | ||
| 40 | |||
| 41 | import matplotlib.pyplot as plt | ||
| 42 | import pandas as pd | ||
| 43 | |||
| 44 | # Read the data | ||
| 45 | df = pd.read_csv("data.csv") | ||
| 46 | |||
| 47 | # Settings | ||
| 48 | plt.title("Connect median NLB vs ALB") | ||
| 49 | plt.tight_layout(pad=2) | ||
| 50 | fig = plt.gcf() | ||
| 51 | fig.set_size_inches(10, 4) | ||
| 52 | |||
| 53 | # Plotting | ||
| 54 | plt.plot(df["Epoch"], df["Connect (ALB)"], label = "ALB", color="black", linestyle="-") | ||
| 55 | plt.plot(df["Epoch"], df["Connect (NLB)"], label = "NLB", color="black", linestyle="--") | ||
| 56 | |||
| 57 | # Adding x and y axis labels | ||
| 58 | plt.xlabel("Epoch", fontstyle="italic") | ||
| 59 | plt.ylabel("Median value (ms)", fontstyle="italic") | ||
| 60 | |||
| 61 | # Legend | ||
| 62 | legend = plt.legend() | ||
| 63 | legend.get_frame().set_linewidth(0) | ||
| 64 | |||
| 65 | # Export as SVG | ||
| 66 | plt.savefig("plot.svg", format="svg") | ||
| 67 | ``` | ||
| 68 | |||
| 69 |  | ||
| 70 | |||
| 71 | The image above is SVG and you can zoom in and out and check that the image is vector. | ||
