0

I have the following PDF produced from matplotlib :

pdf with legend to swap

I have inverted in the legend, during the generation of this plot, the lines "Observed data" and "Model".

I would like to swap them to be right.

From this link :

How to swap text based on patterns at once with sed?

I tried to inspire me and to do in a shell script :

cp -p $1 back_"$1"
qpdf --stream-data=uncompress $1 uncompressed.pd
gsed 's/Observed\ data/zz/g;s/Model/Observed\ data/g;s/zz/Model/g' uncompressed.pdf > temp_uncompressed.pdf
qpdf --stream-data=compress temp_uncompressed.pdf back_"$1"
rm -f uncompressed.pdf temp_uncompressed.pdf
mv -f back_"$1" $1

and execute by simply : $ ./myShell fit.pdf

Unfortunately, the swapping is not performed and I can't see where is the error.

If someone could see what's wrong ...

And from my first tests, this shell script produces warnings or errors and I don't know if I can avoid to get them.

guizmo133
  • 11
  • 3
  • Why can you fix not the Matplotlib script? Probably it's just swapping the `label=...` in the two plotting commands. – gboffi Jul 24 '23 at 08:55
  • since there are a lot of generated PDF plots and also many associated Matplotlib scripts. Regards – guizmo133 Jul 24 '23 at 10:50
  • Do ALL your many associated Matplotlib scripts make the same mistake? I'm amazed. – gboffi Jul 24 '23 at 12:00

1 Answers1

0

Sorry, I have just done it in a simplier way :

cp -p $1 back_"$1"
qpdf --stream-data=uncompress $1 uncompressed.pdf
gsed -i 's/Observed\ data/zz/g' uncompressed.pdf
gsed -i 's/Model/Observed\ data/g' uncompressed.pdf
gsed -i 's/zz/Model/g' uncompressed.pdf
qpdf --stream-data=compress uncompressed.pdf back_"$1"
rm -f uncompressed.pdf
mv -f back_"$1" $1
guizmo133
  • 11
  • 3
  • Assuming that the labels are on different lines of your input, the reason that the OP did not work is because sed is a stream processor, loading one line of input at a time into the pattern buffer, then executing the three commands, then loading the next line, etc. Note that using `zz` as your placeholder is risky if that string appears anywhere else in the pdf. Finally, your OP might have worked had you used `gsed -zi` which would have loaded the entire file into the pattern buffer. – stevesliva Jul 20 '23 at 14:20
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '23 at 05:12