[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy

GMT seems to be a common tool for drawing the epicenter ball (beachball) that represents the focal mechanism of an earthquake, but Python and the visualization library Matplotlib in it. ) Can also be used. As far as I can tell, the main methods for drawing the source sphere using Python are as follows.

  1. Learn how to project the epicenter sphere and draw a script on your own.
  2. With GMT (however> = 6.1.1 [^ 1]) installed in your environment, import one of the Python libraries, PyGMT, and then import it. Call GMT using one of the APIs pygmt.Figure.meca.
  3. Use QGIS
  4. Use __ObsPy (https://docs.obspy.org/), one of the Python libraries. __

As for GMT, there are so many instruction books written by active researchers, and there is already a commentary article for QGIS, so ** Here, I will introduce how to draw a single source sphere for ObsPy in 4. .. ** **

Drawing example some.png

1. Table of contents

1. Table of Contents 2. Purpose of this page 3. Usage environment 4. How to install ObsPy 5. Draw something for the time being 6. Demonstration 7. Conclusion 8. References

2. Purpose of this page

Write a source sphere as a Matplotlib subplot.

3. Usage environment

Matplotlib used the one that was included by default when Anaconda was installed. ObsPy required future, scipy, sqlalchemy, numpy, lxml, setuptools, decorator, matplotlib, requests, etc., so if you get an error during the installation, suspect them. If you install it according to the normal procedure, these will be installed automatically.

4. How to install ObsPy

Officially, it is recommended to install it in a virtual environment using Anaconda [^ 3]. In particular,

  1. Type conda config --add channels conda-forge in the command to add the conda-forge channel to Anaconda's configuration.
  2. Create a virtual environment (environment name: obspy) that runs the python version specified by conda create -n obspy python = 3.8 etc.
  3. Start the virtual environment with conda activate obspy etc.
  4. Install with conda install obspy.

It will be the flow. For details, see "How to create a conda virtual environment" and "How to install a conda package". The above flow is a translation of the following page as it is, so it is strongly recommended to check it before doing it.

Installation via Anaconda · obspy/obspy Wiki · GitHub

In my own environment, I used pip to enter in one shot (because it is a personal computer that can be broken). Type pip install obspy at the command prompt and it handles all the dependencies. However, when building an environment on Windows, we do not recommend it because there are some contents on the net that it is generally better to do it virtually. Also, if you want a higher version, there is a github repository at the link below, so you may be able to install it from there (I have no experience).

GitHub - obspy/obspy: ObsPy: A Python Toolbox for seismology/seismological observatories.

5. Draw something for the time being

While referring to the official tutorial of ObsPy (Link 1, Link 2), try executing the following script. Matplotlib.pyplot.show () didn't work well in my own environment, so I set up to create a PNG image (test.png) for the time being.

test.py


import matplotlib.pyplot as plt
from obspy.imaging.beachball import beach

fig = plt.Figure(figsize=(6,6)) # matplotlib.pyplot.Figure 600x600(px)
np1 = [249,15,127] #Focal mechanism I want to draw Part 1
mt = [-1.2676, 1.8667, -0.5991, 0.7903, 1.6198, -0.9791] #Focal mechanism I want to draw Part 2
beach1 = beach(np1, xy=(25, 25), width=30) 
beach2 = beach(mt, xy=(75, 75), width=50)
ax = fig.add_subplot(111, facecolor="white")
ax.plot([25, 75], [75, 25], "rv", ms=20)
ax.add_collection(beach1)
ax.add_collection(beach2)
ax.set_xlim((0, 100))
ax.set_ylim((0, 100))
fig.savefig("test.png ")
plt.close()

result test.png

If the environment is in place, you should be able to execute it without difficulty. As you can see from the image above, it has the following features.

  1. Arranged so that the center of the epicenter sphere comes to the set coordinate values.
  2. The default color is blue, so if you want to use the commonly used black-and-white display, you have to change the color to black (may differ depending on the environment).
  3. When giving the fault plane composition (strike, dip, slip angle) and moment tensor (mrr (mzz), mtt (mxx), mff (myy), mrt (mxz), mrf (-myz), mtf (- There are two ways to give mxy)).
    * It seems that the formula adopts the above order for the order and orientation of the elements (obspy.imaging.beachball.beach & mdash; ObsPy Documentation (1.2.0)), but reconfirmed. Recommend. In particular, note that there are multiple formats for moment tensors.
  4. The default is probably the lower hemisphere projection (confirmation required).
  5. The size of the source sphere depends on the coordinate values ​​(that is, the source sphere is distorted if the x-axis and y-axis scales are different).

6. Demonstration

Based on the tutorial script, I created a demo script.

beachball_demo.py


import matplotlib.pyplot as plt
from obspy.imaging.beachball import beach

fig = plt.Figure(figsize=(18,6))

#Drawing by strike, tilt, and slip angle
ax1 = fig.add_subplot(1,3,1, facecolor="white") #The background color is white
np1 = [249,15,127] # strike, dip, rake
explanation = "Earthquake\n2003-09-26 04:50" #Text on the epicenter sphere
explanation2 = "strike : " + str(int(np1[0])) + '\n' + "dip : " + str(int(np1[1])) + '\n' + "rake : " + str(int(np1[2])) #Similarly
beach1 = beach(np1, xy=(50, 50), width=50, facecolor="black") # obspy.imaging.Drawing item by beach ball Blue by default in your own environment
ax1.set_aspect("equal")#Align the scale of the xy axis
ax1.add_collection(beach1) #Insert beach ball
ax1.text(50,85,explanation,fontsize=20,horizontalalignment='center',verticalalignment='center') #Text placement. The center of the box(50,85)To be
ax1.text(50,12,explanation2,fontsize=20,horizontalalignment='center',verticalalignment='center') #Similarly
ax1.set_xlim((0, 100)) #For convenience, such coordinate values ​​are given.
ax1.set_ylim((0, 100)) #Similarly
ax1.axes.xaxis.set_visible(False) #Hide scale
ax1.axes.yaxis.set_visible(False) #Similarly

#Drawing with a moment tensor
ax2 = fig.add_subplot(1,3,2, facecolor="white")
mt = [-1.2676, 1.8667, -0.5991, 0.7903, 1.6198, -0.9791] # mrr(mzz) mtt(mxx) mff(myy) mrt(mxz) mrf(-myz) mtf(-mxy)
explanation = "Earthquake\n2020-12-31 23:45"
explanation2 = "mrr : " + str(mt[0]) + '\n' + "mtt : " + str(mt[1]) + '\n' + "mff : " + str(mt[2])
explanation3 = "mrt : " + str(mt[3]) + '\n' + "mrf : " + str(mt[4]) + '\n' + "mtf : " + str(mt[5])
beach2 = beach(mt, xy=(50, 50), width=50, facecolor="red")#Make it red
ax2.set_aspect("equal")
ax2.add_collection(beach2)
ax2.text(50,85,explanation,fontsize=20,horizontalalignment='center',verticalalignment='center')
ax2.text(2,12,explanation2,fontsize=20,horizontalalignment='left',verticalalignment='center') #Adjust the coordinate values ​​to the left
ax2.text(52,12,explanation3,fontsize=20,horizontalalignment='left',verticalalignment='center')
ax2.set_xlim((0, 100))
ax2.set_ylim((0, 100))
ax2.axes.xaxis.set_visible(False)
ax2.axes.yaxis.set_visible(False)

#Appropriate scatter plot unrelated to the earthquake
ax3 = fig.add_subplot(1,3,3, facecolor="white")
ax3.scatter([10, 49, 46, 90, 24], [45, 57, 32, 67, 2],color="black")
ax3.set_aspect("equal")
ax3.set_xlim((0, 100))
ax3.set_ylim((0, 100))
ax3.set_xlabel('x', fontsize=20)
ax3.set_ylabel('y', fontsize=20)
ax3.set_title("Some plot", fontsize=20)

plt.tight_layout()#So that the labels do not overlap
fig.savefig("beachball_demo.png ") #Save as png image
#fig.show() #It did not activate in its own environment
plt.close()

result beachball_demo.png

It seems to work well v (-∀-) v

7. Conclusion

By applying this script, I think you can draw the source sphere in the image plotting other data. However, most of the demand for epicenter spheres is to plot on a map, so if you want to complete them on Python, you need to link with a map library such as Cartopy (I can follow that at the moment). Is not ) . For plotting on maps, ObsPy's official documentation has a script that uses mpl_toolkits.basemap (link), but the new release of mpl_toolkits.basemap that you are using will be stopped in 2020. It is supposed to be [^ 2], and it is recommended to use Cartopy as a successor. I will try it if I have a chance.

Regarding drawing on the map, I think that there are more references in GMT and QGIS at present, so it is recommended to refer to them.

===== 2021/01/15 postscript: It turned out that cooperation with Cartopy is possible. [Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy

8. References

Recommended Posts

[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
A python graphing manual with Matplotlib.
I made a fortune with Python.
I made a daemon with Python
I wrote a program quickly to study DI with Python ①
I made a character counter with Python
I drew a heatmap with seaborn [Python]
I wrote matplotlib
I tried a functional language with Python
What I did with a Python array
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
A memo that I wrote a quicksort in Python
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
I wrote a class in Python3 and Java
[Python] I made a Youtube Downloader with Tkinter.
I want to write to a file with Python
Visualize grib2 on a map with python (matplotlib)
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
Heatmap with Python + matplotlib
I learned Python with a beautiful girl at Paiza # 02
[Python] How to draw a line graph with Matplotlib
I learned Python with a beautiful girl at Paiza # 01
I made a Christmas tree lighting game with Python
I tried to draw a route map with Python
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
Forcibly draw something like a flowchart with Python, matplotlib
I want to work with a robot in python.
I tried to automatically generate a password with Python3
A memo that I touched the Datastore with python
[Python] How to create a 2D histogram with Matplotlib
I want to run a quantum computer with Python
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
I created a stacked bar graph with matplotlib in Python and added a data label
I tried fp-growth with python
I wrote GP with numpy
I wrote python in Japanese
I made blackjack with python!
Make a fortune with Python
I made a python text
I tried gRPC with Python
I tried scraping with python
I made blackjack with Python.
Create a directory with python
I made wordcloud with Python.
Study math with Python: Draw a sympy (scipy) graph with matplotlib
I made a package to filter time series with python
I wrote the basic grammar of Python with Jupyter Lab
Let's create a PRML diagram with Python, Numpy and matplotlib.
I made a simple book application with python + Flask ~ Introduction ~
I made a puzzle game (like) with Tkinter in Python