Matplotlib: Get Rid of White Border - matplotlib

I want to get rid of the white border when I save my image to a png in python.
I tried plt.box(on=None), plt.axis('off'). I tried setting the figure's 'frameon' parameter to false.
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
figure(num=None, figsize=(7.965,7.965), dpi=80,facecolor='none',clear=True)
plt.box(on=None)
plt.axis('off')
plt.imshow(Data, cmap='Greys_r', norm=Norm,origin='lower',aspect='auto',interpolation='nearest')
plt.savefig(locationFITSfolder+fitsFile[:-5],transparent=False,bbox=False)
I want there to be no white border to my image. Transparent.

If you change the parameters to the savefig function, you will get the desired output.
Specifically, you must use transparent=True. Note that bbox=False and frameon=False are optional, and only change the width of transparent space around your image.
Adapting from your sample code:
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
#create sample data
import numpy as np
Data = np.random.random([4,4])
figure(num=None, figsize=(7.965,7.965), dpi=80,facecolor='none',clear=True)
plt.box(on=None)
plt.axis('off')
plt.imshow(Data, cmap='Greys_r',origin='lower',aspect='auto',interpolation='nearest')
plt.savefig(locationFITSfolder+fitsFile[:-5],transparent=True)
(sidenote -- you may wish to use os.path.join, .split, and .splitext for file I/O, instead of slicing string names)
This yields the expected image output: (note that the image has transparent borders when you open it in a new tab or download it).

Related

How to get rid of white slivers between adjecent polygons when plotting a geopandas object?

I am plotting some raster data on a map using geopandas.
Even though all polygons are exactly adjectent, when plotting it appears as if there is space in between them, so it looks like there is a white grid on the plot.
I have tried casting to a different crs, but that didn't change anything. Adding padding to the polygons isn't a great solution, there's nothing wrong with the polygons - I've checked they are exactly adjecent.
Here is an example of runnable code where you can clearly see the problem.
import geopandas as gpd
import matplotlib.pyplot as plt
geometries = gpd.read_file('https://confluence.govcloud.dk/download/attachments/53086340/10x10km-Grid.zip?version=1&modificationDate=1644923591000&api=v2')
geometries.plot()
plt.show()
Which gives this output for me
If you specify the edgecolor parameter to be the same color as the polygons, I believe this will render the way you want.
import geopandas as gpd
geometries = gpd.read_file('https://confluence.govcloud.dk/download/attachments/53086340/10x10km-Grid.zip?version=1&modificationDate=1644923591000&api=v2')
geometries.plot(edgecolor='tab:blue')

How to show legend in missingno matrix?

So far, I have managed to spawn a legend box and have managed to put it outside the chart. But it is showing the same colours for both the labels (white and white) whereas I would prefer it to show white and gray.
import missingno as msno
msno.matrix(X_train, figsize=(15,10), sparkline=False, p=0);
plt.legend(['missing','not missing'],loc='center left', bbox_to_anchor=(1, 0.5))
You'll have to craft the legend by hand. matplotlib has a legend guide showing how you can do this. The section describing "proxy artists" in particular is relevant to your use case. I haven't tested it, but the following should work:
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import missingno as msno
msno.matrix(...your data...)
gray_patch = mpatches.Patch(color='gray', label='Data present')
white_patch = mpatches.Patch(color='white', label='Data absent ')
plt.legend(handles=[gray_patch, white_patch])
plt.show()

how to remove all indicators from pyplot.polar

i need to make a polar plot with just the main data content visible.
for now i have managed to get the following image by using these simple codes.
but there is still one outline circle left around it. how can i remove it
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
data = np.random.randint(1800,2200,(24*60))
data = list(data)
data.append(data[0])
print(data)
theta = np.arange(0,360+360/(24*60),360/(24*60))*np.pi/180
plt.polar(theta, data)
plt.xticks([])
plt.yticks([])
plt.savefig("p.png")
plt.show()
This should do the trick:
plt.box(on=None)
Solution inspired from the Q: Removing frame while keeping axes in pyplot subplots

layout problem of multiple heatmaps in one figure with matplotlib

I put multiple heatmaps in one figure with matplotlib. I cannot layout it well. Here is my code.
import matplotlib; matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(6,240,240)
y = np.random.rand(6,240,240)
t = np.random.rand(6,240,240)
plt.subplots_adjust(wspace=0.2, hspace=0.3)
c=1
for i in range(6):
ax=plt.subplot(6,3,c)
plt.imshow(x[i])
ax.set_title("x"+str(i))
c+=1
ax=plt.subplot(6,3,c)
plt.imshow(y[i])
ax.set_title("y"+str(i))
c+=1
ax=plt.subplot(6,3,c)
plt.imshow(t[i])
ax.set_title("t"+str(i))
c+=1
plt.tight_layout()
plt.savefig("test.png")
test.png looks like this.
I want to
make each heatmap bigger
reduce the margin between each heatmaps in row.
I tried to adjust by "subplots_adjust", but it doesn't work.
Additional information
According to ImportanceOfBeingErnest's comment, I removed tight_layout(). It generated this.
It makes bigger each heatmap, but titles overlappes on subplots. And I still want to make each heatmap more bigger, and I want to reduce the margin in row.

Visualize my matplotlib output

I run a several code and get
Draw = pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')
type(Draw)
<matplotlib.text.Text at 0x7fb063733f90>
How can make my result visible in different ways?
You need to explicitly call pyplot.show() to display the graphics.
import matplotlib.pyplot as plt
pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')
plt.show()