我需要拍摄一张图像并在一些过程后保存.当我显示它时,图形看起来很好,但是当我保存图形时,我在保存的图像周围有一些空白区域.我试过方法的'tight'选项savefig,也没用.代码:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
fig = plt.figure(1)
img = mpimg.imread(path)
plt.imshow(img)
ax=fig.add_subplot(1,1,1)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches=extent)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我试图通过在图上使用NetworkX绘制基本图并保存它.我意识到没有图形它可以工作,但是当添加图形时,我会在保存的图像周围获得空白区域;
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edge(1,3)
G.add_edge(1,2)
pos = {1:[100,120], 2:[200,300], 3:[50,75]}
fig = plt.figure(1)
img = mpimg.imread("C:\\images\\1.jpg")
plt.imshow(img)
ax=fig.add_subplot(1,1,1)
nx.draw(G, pos=pos)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches = extent)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud) 我正在使用mplot3d:
fig = plt.figure(figsize=(14,10))
ax = Axes3D(fig,azim=azimuth,elev=elevation)
ax.grid(on=False)
# Additional axes
xspan = np.linspace(0,80+20)
yspan = np.linspace(0,60+20)
zspan = np.linspace(0,60+20)
ax.plot3D(xspan,np.zeros(xspan.shape[0]),np.zeros(xspan.shape[0]),'k--')
ax.plot3D(np.zeros(yspan.shape[0]),yspan,np.zeros(yspan.shape[0]),'k--')
ax.plot3D(np.zeros(zspan.shape[0]),np.zeros(zspan.shape[0]),zspan,'k--')
ax.text(xspan[-1]+10, .5, .5, "x", color='red')
ax.text(.5, yspan[-1]+10, .5, "y", color='red')
ax.text(.5, .5, zspan[-1]+10, "z", color='red')
NZindices = np.nonzero(t2)[0]
#print "Nonzero values of T^2", len(NZindices), "out of", X.shape[0]
ONZ_X, ONZ_Y, ONZ_Z, ONZ_p = [],[],[],[]
INZ_X, INZ_Y, INZ_Z, INZ_p = [],[],[],[]
# Separate indices I/O
for ind in NZindices:
if ind <= HALF_INDICES:
INZ_X.append( X[ind] )
INZ_Y.append( Y[ind] ) …Run Code Online (Sandbox Code Playgroud) 我正在用一堆多边形绘制一个表面。绘图非常简单,如下所示。
def plotSurface(cell, numOfLayer, name=None, alpha = 0.5):
#import the libraries
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
import matplotlib.pyplot as plt
#limits of the plot
radius = (numOfLayer>1)*(np.sqrt(3.)*(numOfLayer-1)-Length)+Length#the radius of circle to be projected on
#plotting part
fig = plt.figure(frameon=False,figsize=(12,10))
ax = Axes3D(fig)
ax.set_xlim((-2*radius,2*radius))
ax.set_ylim((-2*radius,2*radius))
ax.set_zlim((-0.5*radius,2*radius))
ax.axis('off')
#fig = plt.figure()
#ax = fig.gca(projection='3d')
##iterating through the cell##
for stuff happening here : verts are the polygon vertices
#adding to 3d …Run Code Online (Sandbox Code Playgroud)