Changing the border width of a plot

Question:

I have a script to create an easy plot, but I would like to change the border width because it is a little bit transparent. How can I do this?
With images is clearer the my problem

df_stats
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
ax = plt.axes()

# Increase the plot size and font size.
plt.rcParams["figure.figsize"] = (300,200)
plt.title("Training & Validation Loss",fontsize=500)
plt.xlabel("Epoch", fontsize=500)
plt.ylabel("Loss", fontsize=500)
plt.xticks(list(np.arange(0, 30, 5)))
plt.xticks(fontsize=500)
plt.yticks(fontsize=500)
plt.plot(df_stats['Training Loss'], color='b', marker='.', linestyle='solid', mec='b', markersize=200, markerfacecolor='white', label="Training", linewidth=80)
plt.plot(df_stats['Valid. Loss'], color='g', marker='.', linestyle='solid',  mec='b', markersize=200, markerfacecolor='white',label="Validation", linewidth=80)
plt.grid(True, color='black', linestyle='dashed', linewidth=10, axis='x')
plt.grid(True, color='black', linestyle='dashed', linewidth=10, axis='y')  
plt.legend(fontsize=500, edgecolor='black')

result
more visible the box of the plot

Answers:

I changed the size of your plot because it was way too huge for me to work with, just FYI.

What you can do is access the spines on your figure by using ax.spines. To do this, first you have to place fig, ax = plt.subplots() at the top of your plot code (so that we can use ax). Then, we can change the line width of the spines to whatever we want with set_linewidth(). Unfortunately, I don’t know a good way to include all the spine locations in one line of code, so I had to put it in a for loop:

for location in ['left', 'right', 'top', 'bottom']:
    ax.spines[location].set_linewidth(10)

So, altogether, it would look like this:

plt.rcParams["figure.figsize"] = (30,20)

fig, ax = plt.subplots()
plt.title("Training & Validation Loss",fontsize=50)
plt.xlabel("Epoch", fontsize=50)
plt.ylabel("Loss", fontsize=50)
plt.xticks(list(np.arange(0, 30, 5)), alpha=1)
plt.xticks(fontsize=50)
plt.yticks(fontsize=50)
plt.plot(np.arange(0, 30, 5), color='b', marker='.', linestyle='solid', mec='b', markersize=20, markerfacecolor='white', label="Training", linewidth=8)
plt.plot(np.arange(0, 30, 5), color='g', marker='.', linestyle='solid',  mec='b', markersize=20, markerfacecolor='white',label="Validation", linewidth=8)
plt.grid(True, color='black', linestyle='dashed', linewidth=1, axis='x')
plt.grid(True, color='black', linestyle='dashed', linewidth=1, axis='y')  
plt.legend(fontsize=20, edgecolor='black')
for location in ['left', 'right', 'top', 'bottom']:
    ax.spines[location].set_linewidth(10)

enter image description here

Answered By: Michael S.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.