How to remove white borders on left/right side?
Question:
I have plots, containing technical measurements which are looking like this:
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).
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.
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()
Answers:
To remove the white borders on the left and right sides of a plot in seaborn, you can use the despine function. This function removes the top, right, and left spines (the lines that frame the plot) by default.
sns.despine(left=True, right=True)
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:
I have plots, containing technical measurements which are looking like this:
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).
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.
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()
To remove the white borders on the left and right sides of a plot in seaborn, you can use the despine function. This function removes the top, right, and left spines (the lines that frame the plot) by default.
sns.despine(left=True, right=True)
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()