How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)
First off, if you're using savefig
, be aware that it will override the figure's background color when saving unless you specify otherwise (e.g. fig.savefig('blah.png', transparent=True)
).
However, to remove the axes' and figure's background on-screen, you'll need to set both ax.patch
and fig.patch
to be invisible.
E.g.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
for item in [fig, ax]:
item.patch.set_visible(False)
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
(Of course, you can't tell the difference on SO's white background, but everything is transparent...)
If you don't want to show anything other than the line, turn the axis off as well using ax.axis('off')
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
fig.patch.set_visible(False)
ax.axis('off')
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
In that case, though, you may want to make the axes take up the full figure. If you manually specify the location of the axes, you can tell it to take up the full figure (alternately, you can use subplots_adjust
, but this is simpler for the case of a single axes).
import matplotlib.pyplot as plt
fig = plt.figure(frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.plot(range(10))
with open('test.png', 'w') as outfile:
fig.canvas.print_png(outfile)
Matplotlib : fill subarea of a Graph
As mentioned, you can use axvspan() to add a suitable background colour.
For example:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import random
from datetime import datetime, timedelta
# Create some random data
data = []
x = datetime(2021, 1, 1)
y = 0
day = timedelta(days=1)
for _ in range(100):
x += day
y += random.random()
data.append([x, y])
df = pd.DataFrame(data, columns=['Date', 'Value'])
df.plot('Date', 'Value')
ax = plt.gca()
march = date2num(datetime(2021, 3, 1))
latest = date2num(data[-1][0])
ax.axvspan(xmin=march, xmax=latest, facecolor='silver')
plt.show()
Giving you:
Related Topics
No Output Displays When Execute Python File
Fast Way to Split Column into Multiple Rows in Pandas
How to Map True/False to 1/0 in a Pandas Dataframe
How to Get One Key and Value from a Json in Python
How to Create Different Variable Names While in a Loop
Replacing All Negative Values in Certain Columns by Another Value in Pandas
Using a String Variable as a Variable Name
How to Loop Over Multiple Dataframes and Produce Multiple List
How to Compile Python Script to Binary Executable
How to Make a Function Change Variables While in a While Loop
How to Save All the Variables in the Current Python Session
Print All Number Divisible by 7 and Contain 7 from 0 to 100
Efficient Way of Having a Function Only Execute Once in a Loop
Finding the Index of the First Occurrence of Any Item in a List
How to Convert a 1 Channel Image into a 3 Channel With Opencv2