[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.

At the end of the article I posted earlier (I wrote a beach ball with Matplotlib + ObsPy), the cooperation between ObsPy and the map library Cartopy has not been confirmed. I wrote that, but it turned out that ** it is possible to draw on a map **, so I will describe this.

1. Table of contents

1. Table of Contents 2. Purpose of this page 3. Usage environment [4. How to install Cartopy](#4-Cartopy のインストール方法) 5. Demonstration 6. Conclusion 7. References

2. Purpose of this page

Draw the source sphere of the lower hemisphere projection on the map.

3. Usage environment

cartopy was installed with pip.

4. How to install Cartopy

According to Official description, assuming the environment of Anaconda, it is quick to enter with the conda command conda install -c conda-forge cartopy. Also, if required dependencies is satisfied, you can install it with pip install cartopy. Probably, if you are using Anaconda, installing with pip should automatically resolve this required dependency.

From here on, it's a shame, but if you installed python "directly" on windows 10 via the official installer or store, installing Cartopy can be quite a daunting task. When I tried before switching to Anaconda, when I tried to pip cartopy, it said that the version of proj was low, and when I tried to pip proj, I didn't have the required version (proj4). No, when I try to install proj4, I end up installing OSGeo4w, so it's okay to install OSGeo4w, but how do I use it? I fell into the situation and gave up. Please let me know if there is an article that even I, a beginner, can understand ...

By the way, in Ubuntu, I entered without any problem even if it was not Anaconda. Ubuntu is a good boy.

5. Demonstration

So let's run a suitable script. In My previous work, I found that ObsPy returned a beach ball as a collection of his Matplotlib, so if I knew that I could draw it on a map, I could write it easily.

The beginning of the data used is as follows.

temp.csv


longitude	latitude	depth	strike1	dip1	rake1	strike2	dip2	rake2	mantissa	exponent	type
136.3025	36.2495	11	63	59	94	236	31	84	2.66	21	1
135.5475	35.2028	20	334	85	11	243	79	175	9.92	21	2
136.72	    35.1692	14	3	70	105	145	25	55	3.43	21	1
134.8153	34.4015	11	293	87	28	201	62	177	4.23	21	2

Put this in the same directory as the script below and run it. Regarding drawing a map with Cartopy, please refer to Official script example and This person's blog (I will post it because it was link free ...). I did. If you want to devise a map drawing, please see there.

cartopy_obspy.py



import numpy as np
import pandas as pd
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import matplotlib.ticker as mticker
from obspy.imaging.beachball import beach

#Map and grid drawing
fig = plt.Figure(figsize=(5,4))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree()) # platecarree (Equirectangular projection)Display with
ax.set_extent((134, 137, 33.5, 36.5)) #Area limitation
grids = ax.gridlines(draw_labels=True) #Draw parallels and meridians with labels
grids.xlocator = mticker.FixedLocator(np.arange(120,150,0.5)) # 0.5 degrees each
grids.ylocator = mticker.FixedLocator(np.arange(20,50,0.5)) 
ax.coastlines(resolution='10m') #Draw coastline with a resolution of 10 m
ax.add_feature(cfeature.LAND, color="lightgrey") #Make the land gray

mfile = pd.read_csv("temp.csv") #Data file
mech = mfile[["strike1", "dip1", "rake1"]] #Focal mechanism solution
loc = mfile[["longitude", "latitude"]] #position
typ = mfile[["type"]] #Is it a normal fault type? 0=Normal fault, 1 =Reverse fault, 2 =Strike-slip fault
pallet = ["blue", "red", "lime"] #Color coded by fault type

for i in range(len(loc)) :
    beach1 = beach(mech.iloc[i,:], xy=loc.iloc[i,:], width=0.2,linewidth=0.4,facecolor=pallet[int(typ.iloc[i,0])])
    ax.add_collection(beach1)

fig.savefig("sample.png ")
plt.close(fig)

The result is the same as Opening.

6. Conclusion

It was easy when I tried it. However, at present, this only allows plotting on a map, and we are still looking for a way to draw a source sphere on a vertical section (probably not). In terms of being able to do that, I feel that GMT has an overwhelming degree of freedom. This script may not be useful except in situations where you have to do it in Python.

7. References

Recommended Posts

[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
I drew a heatmap with seaborn [Python]
Map rent information on a map with python
I made a Hex map with Python
Try drawing a map with python + cartopy 0.18.0
Folium: Visualize data on a map with Python
Visualize grib2 on a map with python (matplotlib)
I tried to draw a route map with Python
I made a Python3 environment on Ubuntu with direnv.
A memo that I touched the Datastore with python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I made a fortune with Python.
I liked the tweet with python. ..
I made a daemon with Python
Life game with Python [I made it] (on the terminal & Tkinter)
Control the motor with a motor driver using python on Raspberry Pi 3!
[Python] I wrote the route of the typhoon on the map using folium
I tried using "Asciichart Py" which can draw a beautiful graph on the console with Python.
I made a character counter with Python
A memo with Python2.7 and Python3 on CentOS
Download files on the web with Python
I tried a functional language with Python
What I did with a Python array
[Python] A progress bar on the terminal
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
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I get a Python No module named'encodings' error with the aws command
Install the python module with pip on a server without root privileges
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried "smoothing" the image with Python + OpenCV
[Python] Get the files in a folder with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I tried "differentiating" the image with Python + OpenCV
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Build a python environment with ansible on centos6
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
[Patent analysis] I tried to make a patent map with Python without spending money
[Python] I made a Youtube Downloader with Tkinter.
I tried "binarizing" the image with Python + OpenCV
Introduction to Python with Atom (on the way)
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Decrypt a string encrypted on iOS with Python
I want to write to a file with Python
Create a Python execution environment on IBM i
I tried playing with the calculator on tkinter
I did a little research on the class
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
The world changed when I opened a big Python project (Django) on Sourcetrail (Linux)
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
[Python & SQLite] I analyzed the expected value of a race with horses with a win of 1x ②
I thought about why Python self is necessary with the feeling of a Python interpreter
I learned Python with a beautiful girl at Paiza # 02
I tried Python on Mac for the first time.