Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove white borders on left/right side?

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading