In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "how to realize multi-subgraph sharing color code with python". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Relative humidity and wind fields at 200hPa, 500hPa, 700hPa and 850hPa are plotted using EAR5 hourly data on pressure levels from 1979 to present.
First, we need to create a large canvas and set its size and resolution:
fig = plt.figure(figsize=(12,8),dpi=150)
We need to draw four different layers at the same time, so we set up four sub-images in this big canvas and give them a name:
ax1 = plt.subplot(221, projection = proj)
ax2 = plt.subplot(222, projection = proj)
ax3 = plt.subplot(223, projection = proj)
ax4 = plt.subplot(224, projection = proj)
(I didn't use subplots() here because it doesn't seem to support the projection of ccrs.PlateCarree(), so I'll just have to do it.) And I can't use this method to create a new subgraph, so I can't make the subgraphs share the coordinate axis. I don't know exactly what went wrong.)
However, when calling the function, of course, the name is still the same. Write a little bit ~ If you only draw 4 layers, I use range to control the level of reading. So, how to convert the regular name string into the name of the variable and then pass it into the custom function? I used locals() to do that.
for lev in range(0,4):
r = obj['r'][0][lev][:][:]
u = obj['u'][0][lev][:][:]
v = obj['v'][0][lev][:][:]
the_string = f'ax{lev+1}'
ax = locals()[the_string]
ac = RHLevel(r, u, v, ax, lev)
Why make the function equal to "ac"? Since all four images use the same coloring rules, it's much better to have only one colorbar as a legend in order to look good. So, we can pass out a statement that draws relative humidity and use that information outside the loop to draw a colorbar for the entire graph.
def RHLevel(r, u, v, ax, lev):
......
ac = ax.contourf(lons[248:288,420:480],lats[248:288,420:480],r[248:288,420:480],levels=np.arange(60,110,10),
cmap='Greens',extend='both', alpha=0.9)
return ac
Because the information of ac is returned, the function has a "result" after calling the function. You can "name" the result, so that you can customize the colorbar with the result passed outside the loop:
ac = RHLevel(r, u, v, ax, lev)
cb = fig.colorbar(ac, cax = cbar_ax,orientation='horizontal',spacing='proportional')
Then, you can use the colorbar command to define its position and orientation, where l, b, w, h refer to the left starting point, the lower starting point, the width and the height of the colorbar respectively, and the number is a percentage, for example, 0.25 refers to 0.25 times the width of the large canvas:
l,b,w,h = 0.25, 0.05, 0.5, 0.02
rect = [l,b,w,h]
cbar_ax = fig.add_axes(rect)
cb = fig.colorbar(ac, cax = cbar_ax,orientation='horizontal',spacing='proportional')
To indicate the hierarchy of different images, we can set a subtitle for each subimage in the function. loc =''sets the alignment of the title, where the title is aligned to the left.
ax.set_title(f'{level[lev]}hPa',fontsize=12,loc='left')
Set the title of the whole picture in the big picture (outside the loop)(different from the title of the sub-picture). What is the difference? You can search for the difference between fig/ax/plt. There are many articles that are very detailed:
plt.suptitle(f'May 21,2020 00:00 (UTC) Relative humidity and wind field of each layer', fontsize=18) The content of "How to realize multi-subgraph sharing color code with python" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.