当前位置:网站首页>Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
2022-04-23 10:49:00 【The big pig of the little pig family】
Shared drawing area problem
One . Preface
Sometimes we will find that if the range of the axis is not appropriate, it is difficult to extract the corresponding information from the picture , For example, the figure on the left and the figure below, it is obvious that we can't draw the difference between the density of the two . In addition, many times we want to show two different drawings under a coordinate axis system , But the axis fields of the two are different , What to do at this time ?
Let's take another example , Let's say we want to draw a on a coordinate axis sin Function and a cos function , Obviously, the following program can meet our needs .
fig, ax = plt.subplots(figsize=(10, 8))
x = np.linspace(0, 2*np.pi, 100)
ax.plot(np.sin(x), lw=2, color='c', label=r'$\sin({x})$')
ax.plot(np.cos(x), lw=2, color='k', label=r'$\cos({x})$')
ax.axhline(0, lw=4, color='r')
plt.grid(color='gray', ls=":")
plt.legend(loc='upper right', title=r'$\sin({x})$ and $\cos({x})$ Example ', title_fontsize=12, shadow=True, fancybox=True, framealpha=0.8)
plt.show()
The drawing results are as follows :
Next , Suppose we use the same method to draw 20*sin(x) and cos(x) So what happens ? Please look at the chart below.
We can see that in order to display the complete sinusoidal function normally , The entire cosine function has been compressed , Obviously, this will not achieve the effect we want . But we can see from observation , The definition domain of the two is the same , Only the value range is different , In other words, the two can share one X Axis , But you can't use the same Y Axis , This is the problem we need to solve , It is also the practical significance of sharing the coordinate axis of a single region . This paper mainly discusses the solutions to these two kinds of problems , namely Share the coordinate axis of a single area and Share the axes of the drawing areas of different subareas . The code in this article only provides the key part of the code , The import of necessary libraries and the initialization settings of libraries have been omitted .
Two . Solution
2.1 Axis solutions that share a single region
Through the above example, we already know that the core of solving this problem is how to share X Axis without sharing Y Axis . We can use twinx()
Method to create and return a shared X The second axis system of the axis . Here's the definition of the method :
twinx(ax=None)
Parameters 1:ax: Specify which axis system to share
It should be noted here that the new axis system will cover the old axis system , Its Y Generate ridge labels at the scale axis and scale line . For the question just now, the code using this method is as follows :
fig, ax = plt.subplots(figsize=(10, 8))
x = np.linspace(0, 2*np.pi, 100)
line1 = ax.plot(20 * np.sin(x), lw=2, color='c', label=r'$\sin({x})$')
ax.axhline(0, lw=4, color='r')
ax2 = ax.twinx()
line2 = ax2.plot(np.cos(x), lw=2, color='k', label=r'$\cos({x})$')
plt.grid(color='gray', ls=":")
fig.suptitle(r'$\sin({x})$ and $\cos({x})$ Example ', fontsize=20)
plt.show()
The drawing results are as follows :
You can see that the two share X But each has its own Y Axis , The larger axis will shrink to meet the smaller axis . In this way, we can also draw in a single area in the face of two maps with large data gap .
2.1 Axis solutions that share drawing areas of different subareas
There are two solutions for different drawing areas sharing different subareas , The first is to use set_xlim()
and set_ylim()
Method to manually modify the coordinate axis range , Of course, modifying shared areas is just the tip of the iceberg of these two methods , It doesn't prevent us from achieving the effect we want . The sample code is as follows :
x1 = np.linspace(-2*np.pi, 2*np.pi, 500)
y1 = np.cos(x1**2)
x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)
x3 = np.random.randint(0, 10, 50)
y3 = np.linspace(0, 3, 50)
x4 = np.random.randint(0, 25, 50)
y4 = np.linspace(0, 20, 50)
fig, ax = plt.subplots(1, 2)
fig.suptitle(' Use set_xlim and set_ylim Method ', fontsize=25)
ax[0].scatter(x3, y3)
ax[0].set_title(" Modify the scope according to the figure on the right ", fontsize=15)
ax[1].scatter(x4, y4)
ax[1].set_title('X Axis value :[-2, 25],Y Axis value :[-2, 20]', fontsize=15)
plt.subplots_adjust(wspace=0.6, top=0.8)
ax[0].set_xlim((-2, 25))
ax[0].set_ylim((-2, 20))
ax[1].set_xlim((-2, 25))
ax[1].set_ylim((-2, 20))
plt.show()
The drawing results are as follows :
From the above figure, we can easily see which of the two is more dense , Compared with the previous error examples, the visual effect of this time has been greatly improved .
The second solution is to specify the sharing mechanism at the beginning , It's about delivering sharex
and sharey
Parameters to subplots
Method . The sample code is as follows :
x1 = np.linspace(-2*np.pi, 2*np.pi, 500)
y1 = np.cos(x1**2)
x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)
x3 = np.random.randint(0, 10, 50)
y3 = np.linspace(0, 3, 50)
x4 = np.random.randint(0, 25, 50)
y4 = np.linspace(0, 20, 50)
fig, ax = plt.subplots(1, 2, sharey=True, sharex=True)
fig.suptitle(' Use set_xlim and set_ylim Method ', fontsize=25)
ax[0].scatter(x3, y3)
ax[0].set_title(" Modify the scope according to the figure on the right ", fontsize=15)
ax[1].scatter(x4, y4)
ax[1].set_title('X Axis value :[-2, 25],Y Axis value :[-2, 20]', fontsize=15)
plt.subplots_adjust(wspace=0.6, top=0.8)
plt.show()
The drawing results are as follows :
It can be seen that the effects of these two methods are consistent . These two parameters have a total of five valid values , Namely :
- True/‘all’: All subareas share the same coordinate system
- ‘col’: Each column in the specified sub area wants to have the same coordinate system
- ‘row’: Each row in the specified sub area wants to have the same coordinate system
- False/‘none’: Do not share
- ax: Specify a sub area , In this way, any two sub areas can be specified separately to share the coordinate axis system
Be sure to remember that the shared coordinate system is the maximum of all subareas . That is, there may be X Axis and Y When the axis does not come from a sub area .
3、 ... and . Reference resources
版权声明
本文为[The big pig of the little pig family]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230617063736.html
边栏推荐
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
- Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
- SQL server query database deadlock
- Kaggle - real battle of house price prediction
- Charles function introduction and use tutorial
- VScode
- 1. Sum of two numbers (hash table)
- Arbitrary file reading vulnerability exploitation Guide
- 定义链表(链表)
- 二叉树的构建和遍历
猜你喜欢
JVM - common parameters
IDEA——》每次启动都会Indexing或 scanning files to index
解决方案架构师的小锦囊 - 架构图的 5 种类型
Solution architect's small bag - 5 types of architecture diagrams
Xshell+Xftp 下载安装步骤
【leetcode】107.二叉树的层序遍历II
Let the LAN group use the remote device
Notes on concurrent programming of vegetables (V) thread safety and lock solution
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
Charles 功能介绍和使用教程
随机推荐
Reading integrity monitoring techniques for vision navigation systems - 3 background
Solve the problem of installing VMware after uninstalling
The courses bought at a high price are open! PHPer data sharing
SSH利用私钥无密钥连接服务器踩坑实录
Chapter 120 SQL function round
Pytorch implementation of transformer
Download and installation steps of xshell + xftp
Construction and traversal of binary tree
Full stack cross compilation x86 completion process experience sharing
微信小程序中app.js文件、组件、api
206. Reverse linked list (linked list)
19. Delete the penultimate node of the linked list (linked list)
Yarn core parameter configuration
SQL Server cursor circular table data
Qinglong panel pull library command update [April 20, 2022] collection is not lost
Jerry's factors that usually affect CPU performance test results are: [article]
Derivation and regularization
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
349、两个数组的交集