0

I found that when the text contains a lot of spaces, the output of print() will be different from the output of plt.annotate()

My question is : How can I make the output of annotate() the same as print() in matplotlib?

I use the following code to compare the output of print() with the output of plt.annotate():

from matplotlib import pyplot as plt
import json

with open("epoch_3350-20230218172431.json", "r") as source:
    log_dict = json.load(source)
    print(log_dict["model_structure"])
    plt.figure(figsize=(25, 10))
    plt.annotate(text=f"{log_dict.get('model_structure').__str__()}",
                   xy=(0.08, 0.5), bbox={'facecolor': 'green', 'alpha': 0.4, 'pad': 5},
                   fontsize=14, xycoords='axes fraction', va='center')
    plt.show()
    plt.close()

output of print() :

MTSCorrAD(
  (gin_convs): ModuleList(
    (0): GINConv(nn=Sequential(
      (0): Linear(in_features=1, out_features=3, bias=True)
      (1): BatchNorm1d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): Linear(in_features=3, out_features=3, bias=True)
      (4): ReLU()
    ))
  )
  (gru1): GRU(3, 8)
  (lin1): Linear(in_features=8, out_features=3, bias=True)
)
====================================================================================================
+-------------------------+-----------------------------+-----------------+----------+
| Layer                   | Input Shape                 | Output Shape    |   #Param |
|-------------------------+-----------------------------+-----------------+----------|
| MTSCorrAD               | [792, 1], [2, 52272], [792] | [3]             |      363 |
| ├─(gin_convs)ModuleList | --                          | --              |       24 |
| │    └─(0)GINConv       | [792, 1], [2, 52272]        | [792, 3]        |       24 |
| ├─(gru1)GRU             | [12, 3]                     | [12, 8], [1, 8] |      312 |
| ├─(lin1)Linear          | [8]                         | [3]             |       27 |
+-------------------------+-----------------------------+-----------------+----------+

output of plt.annotate() : enter image description here

As the results show, the output of print() is different from the output of plt.annotate().

PS.

tdy
  • 36,675
  • 19
  • 86
  • 83
theabc50111
  • 421
  • 7
  • 16
  • 2
    Have you tried to use a monospace font? ```plt.annotate( <#What you already have#>, fontfamily='monospace')``` – Oniow Feb 22 '23 at 04:58
  • 1
    You will need to use a monospace font, please refer to [how to change fonts in matplotlib](https://stackoverflow.com/questions/21321670/how-to-change-fonts-in-matplotlib-python) – metatoaster Feb 22 '23 at 05:02

1 Answers1

1

Just like @Oniow said in the comment, the output of plt.annotate() become normal after set the argument: fontfamily='monospace'.

from matplotlib import pyplot as plt
import json

with open("epoch_3350-20230218172431.json", "r") as source:
    log_dict = json.load(source)
    print(log_dict["model_structure"])
    plt.figure(figsize=(25, 10))
    plt.annotate(text=f"{log_dict.get('model_structure').__str__()}",
                   xy=(0.08, 0.5), bbox={'facecolor': 'green', 'alpha': 0.4, 'pad': 5},
                   fontsize=14, fontfamily='monospace', xycoords='axes fraction', va='center')
    plt.show()
    plt.close()
theabc50111
  • 421
  • 7
  • 16