当前位置:网站首页>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
边栏推荐
- VScode
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- MySQL how to merge the same data in the same table
- CentOS/Linux安装MySQL
- Restful、SOAP、RPC、SOA、微服务之间的区别
- 微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
- 微信小程序中app.js文件、组件、api
- VIM usage
- Define linked list (linked list)
- [Niuke challenge 47] C. conditions (BitSet acceleration Floyd)
猜你喜欢
MapReduce compression
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
/Can etc / shadow be cracked?
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
A diary of dishes | 238 Product of arrays other than itself
Swagger2 接口如何导入Postman
【leetcode】102.二叉树的层序遍历
Notes on concurrent programming of vegetables (V) thread safety and lock solution
C language - custom type
随机推荐
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
Swagger2 自定义参数注解如何不显示
What if Jerry's function to locate the corresponding address is not accurate sometimes? [chapter]
997、有序数组的平方(数组)
解决方案架构师的小锦囊 - 架构图的 5 种类型
/Can etc / shadow be cracked?
Hikvision face to face summary
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
What are Jerry's usual program exceptions? [chapter]
UDP basic learning
Linked list intersection (linked list)
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
HuggingFace
Comparison and practice of prototype design of knowledge service app
206、反转链表(链表)
59、螺旋矩阵(数组)
JVM——》常用参数
The courses bought at a high price are open! PHPer data sharing
19、删除链表的倒数第N个节点(链表)