kip-hart / MicroStructPy

Microstructure modeling, mesh generation, analysis, and visualization.
https://docs.microstructpy.org
MIT License
69 stars 19 forks source link

Better plot interface for Python script and Jupyter notebook #14

Closed BinWang0213 closed 4 years ago

BinWang0213 commented 4 years ago

Thanks for your nice Python code for such microstructure modeling.

Is your feature request related to a problem? Please describe. CLI interface can plot very nice seeds, polymesh and trimesh. But I need write very long code to reproduce the figure, here is an examples

def getPhaseColor(i,phases):
    return phases[i].get('color', 'C' + str(i % 10))

# Create Figure
k = 1
off = 1
len_x = domain.length + 2 * off
len_y = domain.width + 2 * off
plt.figure(figsize=(k * len_x, k * len_y))

# Plot Seeds
seed_colors=[getPhaseColor(s.phase, phases) for s in seeds]
seeds.plot(color=seed_colors, alpha=0.8, edgecolor='k', linewidth=0.3)
domain.plot(facecolor='none', edgecolor='k', linewidth=0.5)

# Add legend
legend_patchs=[]
for ph in phases:
     legend_patchs.append( mpatches.Patch(color=ph["color"], ec='k',label=ph["name"]) )
plt.legend(handles=legend_patchs)
plt.axis('image')
plt.show()

Describe the solution you'd like

# Create Figure
plt.figure(figsize=(5, 5))

seeds.plot(material_name=['Matrix','Void'],color=['C0','C1'], alpha=0.8, edgecolor='k', linewidth=0.3)
domain.plot(facecolor='none', edgecolor='k', linewidth=0.5)

plt.axis('image')
plt.show()
kip-hart commented 4 years ago

Hi Bin,

That makes perfect sense. I could move the CLI code into plot methods for the classes. Do you have a deadline you're trying to meet? I'm currently working on another part of my thesis and was planning to get back to this after I defend. I can tackle this sooner though if needed.

Best, Kip

BinWang0213 commented 4 years ago

Hi Kip,

Thanks for your helps. There is no deadline for this . Now I can do it by digging out the CLI code. But the simplified interface can help other new users like me.

BTW, I'm working on meshing the generated micro-structure from your library using Gmsh. I may have more idea to improve your code once I'm finished.

Thanks again for your nice library, Bin

kip-hart commented 4 years ago

Hi Bin,

Thank you so much! I started looking at this a little more and realized it won't be too bad to implement right now.

I'd like to hear your ideas on using gmsh. I have also started using it in my work, since Triangle and TetGen make the mesh super refined in some corners.

I'll keep you posted on the plot changes, but I've started a branch (https://github.com/kip-hart/MicroStructPy/tree/dev-plot-methods).

Best, Kip

kip-hart commented 4 years ago

These features have been added to the source code and I created a new minor release for them (v1.2.0). I added two keyword arguments to the plot methods, material and index_by. From your example, you can use these like:

seeds.plot(material=['Matrix','Void'], color=['C0','C1'], ..., index_by='material')

Please let me know if you get the chance to try it out (described in more detail in #17) and feel free to re-open if there is still an issue.