current position:Home>Python actual combat case, mplcyberpunk module, python realizes cyberpunk data visualization

Python actual combat case, mplcyberpunk module, python realizes cyberpunk data visualization

2022-02-02 08:09:42 Dai mubai

Time is not negative , Create constantly , This article is participating in 2021 Year end summary essay contest

Preface

utilize Python Realize cyberpunk data visualization , I don't say much nonsense ~

Let's start happily ~

development tool

Python edition : 3.6.4

Related modules :

mplcyberpunk modular ;

matplotlib modular ;

numpy modular ;

As well as some Python Built in modules .

Environment building

install Python And add to environment variable ,pip Install the relevant modules required .

Cyberpunk 2077》 Is a by CD Projekt RED Developed action character games

1.png

The boundary between virtual reality and reality is blurred , Artificial intelligence and man-made people can't be distinguished , The poor and the rich are extremely divided , Terrorism is everywhere .

This issue mainly introduces you to a “ Cyberpunk ” Style Python library ,mplcyberpunk

adopt pip install

#  install 
pip install mplcyberpunk
 Copy code 

Example 1

import matplotlib.pyplot as plt
import mplcyberpunk

#  Add the style 
plt.style.use("cyberpunk")

plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')
plt.plot([4, 5, 5, 7, 9, 8, 6], marker='o')

#  Set the line to glow + Area map 
mplcyberpunk.add_glow_effects()
plt.show()
 Copy code 

The area of the line

2.png

By viewing the source file of the library core.py, Discover two main functions of the library

3.png

Use each method once

import numpy as np
import mplcyberpunk
import matplotlib.pyplot as plt

plt.style.use("cyberpunk")

#  data 
x = np.arange(-7, 7, 0.1)
y1 = np.sin(x)
y2 = np.sin(x) + x
y3 = np.sin(x) * x
y4 = np.sin(x) / x
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.plot(x, y4)

#  Line luminescence 
mplcyberpunk.make_lines_glow()
#  Area map 
mplcyberpunk.add_underglow()

#  Save the picture 
plt.savefig("defalut.png")
 Copy code 

Second, the default format

4.png

Add line glow

5.png

Line luminescence + Area map

6.png

import matplotlib.pyplot as plt
from matplotlib import cm
import mplcyberpunk
import numpy as np

# time
t = np.arange(0, 6.4, 0.1)
# frequency
f = 1
amplitudes = np.arange(-10, 11, 1)
# amplitude
A = [x * np.cos(f*t) for x in amplitudes]

#  Set color style ,cool、spring、winter、tab20、coolwarm
colormap_sect = np.linspace(0, 1, len(amplitudes))
colors = [cm.coolwarm(x) for x in colormap_sect]

plt.rcParams['figure.figsize'] = [6, 4]
plt.style.use("cyberpunk")
plt.xlim(right=6.3)

for i in range(21):
    plt.plot(t, A[i], color=colors[i])
mplcyberpunk.make_lines_glow()

#  Title name ,cool、spring、winter、tab20、coolwarm
plt.title("Colormap: 'coolwarm'")
plt.savefig("colormap.png")
 Copy code 

5 A chart of color schemes

7.png

8.png

9.png

10.png

11.png

What we use is ridge_map A map of the ridge

#  install 
pip install ridge_map
#  Dependency of installation Library 
pip install scikit-image==0.14.2
 Copy code 

We need to pay attention to , install scikit-image You need to specify 0.14.2 edition , Otherwise, an error will be reported

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
from ridge_map import RidgeMap
import mplcyberpunk

#  Cyberpunk style 
plt.style.use("cyberpunk")

#  Chinese display 
plt.rcParams['font.sans-serif'] = ['SimHei']  # Windows
plt.rcParams['font.sans-serif'] = ['Hiragino Sans GB'] # Mac
plt.rcParams['axes.unicode_minus'] = False

#  typeface 
font_prop = fm.FontProperties(fname=" Fang Zheng LAN Ting published black .ttf")

#  get data , Special skills are needed here to succeed 
rm = RidgeMap(bbox=(-156.250305, 18.890695, 154.714966, 20.275080), font=font_prop)

#  Set the number of lines , toward , And other properties 
values = rm.get_elevation_data(num_lines=200, viewpoint='north')
values = rm.preprocess(values=values,
                       water_ntile=10,
                       vertical_ratio=240)

#  Set title , line color , Background color, etc 
rm.plot_map(values, label=" Hawaii ", kind='gradient', line_color=plt.get_cmap('spring'), background_color='#212946')
plt.savefig(' Hawaii .png')
 Copy code 

Ridge map of Hawaii Island , Punk style is full

12.png

World map and European map

13.png

14.png

Taiwan Province

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
from ridge_map import RidgeMap
import mplcyberpunk

#  Cyberpunk style 
plt.style.use("cyberpunk")

#  Chinese display 
plt.rcParams['font.sans-serif'] = ['SimHei']  # Windows
plt.rcParams['font.sans-serif'] = ['Hiragino Sans GB'] # Mac
plt.rcParams['axes.unicode_minus'] = False

#  typeface 
font_prop = fm.FontProperties(fname=" Fang Zheng LAN Ting published black .ttf")

#  get data , Special skills are needed here to succeed 
rm = RidgeMap(bbox=(122.014, 25.344, 120.036, 21.902), font=font_prop)

#  Set the number of lines , toward , And other properties 
values = rm.get_elevation_data(num_lines=200, viewpoint='north')
values = rm.preprocess(values=values,
                       water_ntile=10,
                       vertical_ratio=240)

#  Set title , line color , Background color, etc 
rm.plot_map(values, label=" Taiwan Province ", kind='gradient', line_color=plt.get_cmap('spring'), background_color='#212946')
plt.savefig(' Taiwan Province .png')
 Copy code 

Latitude and longitude coordinates , It can be obtained through Tencent location service

15.png

Ridge line map

16.png

copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020809407686.html

Random recommended