1

I have plots, containing technical measurements which are looking like this:

enter image description here

Now I want to remove everything but the content and I managed to get to this point you can see in the following image. But I'm still having the borders on the right and left side. (I changed the background color to yellow for better visibility and highlighted the borders I want to get rid of with red. Actually, they are white).

enter image description here

How can I remove them (the bottom and top don't need to be removed), so that the plot begins exactly where the line starts/ends?

Target image should still be 480x480px, even without the borders.

enter image description here

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"x":[1,2,3,4,5,6,7,8,9,10],
                   "y1":[4,3,2,6,6,2,1,5,7,3],
                   "y2":[1,0,6,0,9,2,3,5,4,7]})

size=[480,480]

fig, ax = plt.subplots(figsize=(size[0]/100, size[1]/100), dpi=100, facecolor="yellow")

p2 = sns.lineplot(data=df, x="x", y="y1", ax=ax)
p2.set(xlabel=None, xticklabels=[],
       ylabel=None, yticklabels=[])
p3 = sns.lineplot(data=df, x="x", y="y2", ax=ax)
p3.set(xlabel=None, xticklabels=[],
       ylabel=None, yticklabels=[])
ax.set(ylim=(0, 10))
plt.box(False)
plt.tick_params(left=False, labelleft=False, bottom=False)
plt.tight_layout()
plt.show()
Marco_CH
  • 3,243
  • 8
  • 25
  • I can't see any red borders using your code. Everything looks good. – コリン Dec 30 '22 at 09:14
  • The red borders show what part I want to get rid of. They are actually white. As I wrote the yellow background is for better visibility, the red part is for highlighting. – Marco_CH Dec 30 '22 at 09:16

1 Answers1

2

References:

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xmargin.html

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html

Using ax.set_xmargin(0) with plt.tight_layout(pad=0), the result below is as close as I can get. The size still remains at 480x480.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"x": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                   "y1": [4, 3, 2, 6, 6, 2, 1, 5, 7, 3],
                   "y2": [1, 0, 6, 0, 9, 2, 3, 5, 4, 7]})

size = [480, 480]

fig, ax = plt.subplots(figsize=(size[0] / 100, size[1] / 100), dpi=100, facecolor="yellow")

p2 = sns.lineplot(data=df, x="x", y="y1", ax=ax)
p2.set(xlabel=None, xticklabels=[],
       ylabel=None, yticklabels=[])
p3 = sns.lineplot(data=df, x="x", y="y2", ax=ax)
p3.set(xlabel=None, xticklabels=[],
       ylabel=None, yticklabels=[])
ax.set(ylim=(0, 10))
plt.box(False)
plt.tick_params(left=False, labelleft=False, bottom=False)

ax.set_xmargin(0)
plt.tight_layout(pad=0)

plt.show()

Result:

enter image description here

コリン
  • 1,005
  • 1
  • 5
  • 26
  • Thank you very much, but there are stil some minor borders. My goal is really to bring the border exactly to the start/end of the line. – Marco_CH Dec 30 '22 at 09:38
  • I update the answer with `ax.set_xmargin(0)` and `plt.tight_layout(pad=0)`. Now it should work. – コリン Dec 30 '22 at 09:39
  • Thanks again for your effort. I still have this border and additionally got this message `/tmp/ipykernel_122434/757902069.py:24: UserWarning: This figure was using constrained_layout, but that is incompatible with subplots_adjust and/or tight_layout; disabling constrained_layout. plt.tight_layout(pad=0)` – Marco_CH Dec 30 '22 at 09:40
  • but if I use savefig, it looks fine. So I accept this answer, thank you again! – Marco_CH Dec 30 '22 at 09:41
  • Sorry, when I test the code, I tried various methods and forgot to remove `layout='constrained'` on line 11. – コリン Dec 30 '22 at 09:42