Last active
September 29, 2018 03:05
-
-
Save falgon/e867c610ebdf957a827e2d26bd2ea451 to your computer and use it in GitHub Desktop.
The figure of cube and its section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import use | |
use('AGG') | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.patches import Rectangle | |
from mpl_toolkits.mplot3d import Axes3D | |
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection | |
class cube_fig: | |
def __init__(self, fname, rectangle): | |
self.fname = fname | |
self.rectangle = rectangle | |
def __enter__(self): | |
return self | |
def config(self): | |
plt.rc('text', aa=True, usetex=True) | |
self.__make_rectangle() | |
self.__make_figs() | |
def __make_rectangle(self): | |
rectangle_pt = [ | |
np.array(list(item)) | |
for item in self.rectangle | |
] | |
vectors = [] | |
for i in range(1, 4): | |
vectors.append(rectangle_pt[i] - rectangle_pt[0]) | |
points = rectangle_pt | |
points += [rectangle_pt[0] + vectors[0] + vectors[1]] | |
points += [rectangle_pt[0] + vectors[0] + vectors[2]] | |
points += [rectangle_pt[0] + vectors[1] + vectors[2]] | |
points += [rectangle_pt[0] + vectors[0] + vectors[1] + vectors[2]] | |
points = np.array(points) | |
edges = [ | |
[points[0], points[3], points[5], points[1]], | |
[points[1], points[5], points[7], points[4]], | |
[points[4], points[2], points[6], points[7]], | |
[points[2], points[6], points[3], points[0]], | |
[points[0], points[2], points[4], points[1]], | |
[points[3], points[6], points[7], points[5]] | |
] | |
ax = plt.subplot2grid((2,2), (0,0), colspan=2, projection='3d') | |
ax.set_title('Cube and its sectionional view') | |
faces = Poly3DCollection(edges, linewidths=1, edgecolors='k') | |
faces.set_facecolor((0, 0, 1, 0.1)) | |
ax.add_collection3d(faces) | |
ax.scatter(points[:,0], points[:,1], points[:,2], s=0) | |
ax.set_aspect('equal') | |
def __common_setup(self, title, ax, xy, w, h): | |
ax.set_title(title, y=1.2) | |
ax.add_patch(Rectangle(xy=xy, width=w, height=h, alpha=0.8)) | |
ax.set_aspect('equal', adjustable='box') | |
def __make_rectangular(self, ax, xy, w, h): | |
ax.grid(which="both") | |
ax.set_xlabel(r"$x$") | |
ax.set_ylabel(r"$y$") | |
limx = [xy[0], w + 0.5] | |
ax.set_xlim(limx) | |
ax.set_xticks(np.arange(limx[0], limx[1], 0.5)) | |
ax.xaxis.set_ticklabels([]) | |
limy = [xy[1], h + 0.5] | |
ax.set_ylim(limy) | |
ax.set_yticks(np.arange(limy[0], limy[1], 0.5)) | |
ax.yaxis.set_ticklabels([]) | |
self.__common_setup('Rectangular coordinate', ax, xy, w, h) | |
def __make_polar(self, ax, xy, w, h): | |
ax.yaxis.set_ticklabels([]) | |
ax.set_thetamin(0) | |
ax.set_thetamax(90) | |
self.__common_setup('Polar coordinate', ax, xy, w, h) | |
def __make_figs(self): | |
xy = [self.rectangle[0][0], self.rectangle[0][1]] | |
w, h = self.rectangle[2][0] - self.rectangle[0][0], self.rectangle[1][1] - self.rectangle[0][1] | |
self.__make_rectangular(plt.subplot2grid((2, 2), (1, 1)), xy, w, h) | |
self.__make_polar(plt.subplot2grid((2,2), (1,0), projection='polar'), xy, w, h) | |
def __exit__(self, exception_t, exception_v, traceback): | |
plt.tight_layout() | |
plt.savefig(self.fname) | |
def main(): | |
rectangle = [ | |
(0,0,0), (0,1,0), (1,0,0), (0,0,1) | |
] | |
with cube_fig('cube_and_cross_section.png', rectangle) as m: | |
m.config() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考: https://stackoverflow.com/questions/44881885/python-draw-3d-cube