Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Ridge Map using python data Visualization Seaborn

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use python data visualization Seaborn to draw ridge map, the article is very detailed, has a certain reference value, interested friends must read it!

1. Introduction

Ridge maps generally consist of vertically stacked line charts in which the line areas overlap each other and share the same x-axis.

Ridge maps are often presented in the form of a relatively uncommon compact map that is very suitable for attracting attention. Looking at the image above, it is appropriate to call it Ridge plot, because the above chart does look like the back of a mountain. In addition, the above image is also called 喜悦 Plots-, mainly because 喜悦 Division adopted this visualization form on the cover of the following album.

two。 Take a chestnut.

After introducing the origin and background of the ridge map, let's give an example. We use the following dataset, which mainly contains the works of Netflix and the corresponding IMDB scores.

Import matplotlib.pyplot as pltimport seaborn as snsimport pandas as pddf = pd.read_csv ('. / data/film.csv') languages = ['English',' Hindi', 'Spanish',' French', 'Italian',' Portuguese'] df_filtered = df [df ['Language'] .isin (languages)] df_filtered

The running results are as follows:

In the above table, from left to right, they are ID, movie name, movie type, premiere date, film length, IMDB score, and movie language.

Next, we first use the Seaborns FacetGrid library to create a probability density distribution graph under different IMDB for each language category of movies. To achieve this function is very simple, only need to the corresponding name fields in the data table to group statistics by value.

The code is as follows:

Sns.set_theme () g = sns.FacetGrid (df_filtered, row= "Language") g.map_dataframe (sns.kdeplot, x = "IMDB Score") g.set (ylabel= "")

The results are as follows:

The above implementation uses the default parameter configuration, the horizontal axis represents the IMDB score, and the vertical axis represents the probability of corresponding films in different languages under different IMDB scores. From the above illustration, we can see the probability density distribution of movie scores in a single language, but it is difficult to see the contrastive distribution among different languages.

Then we try to improve the display by setting the corresponding parameters in the FacetGrid function to make the chart wider and shorter.

The code is as follows:

Sns.set_theme () g = sns.FacetGrid (df_filtered, row= "Language", aspect=9, height=1.2) g.map_dataframe (sns.kdeplot, x = "IMDB Score") g.set (ylabel= "")

The results are as follows:

Although the above improvements can make the comparison between the data more obvious, the visualization is not very attractive in terms of visual effects.

Looking at the image above, we don't actually pay attention to the Y-axis information on the left, we pay more attention to the shape of the data. This means that we can delete the Y axis here.

The code is as follows:

Sns.set_theme () g = sns.FacetGrid (df_filtered, row= "Language", aspect=9, height=1.2) g.map_dataframe (sns.kdeplot, x = "IMDB Score") g.set_titles (") g.set (yticks= [], ylabel=") g.despine (left=True)

The running results are as follows:

3. Ridge map

After our optimization, the IMDB score probability density distribution of the above films in different languages is not intuitive enough.

Next, let's introduce our magic weapon-ridge map step by step.

First, we need to make sure that the background is transparent.

Sns.set_theme (, rc= {"axes.facecolor": (0,0,0,0)})

Next, we need to fill the inner area of the line.

G.map_dataframe (sns.kdeplot, x = "IMDB Score", fill=True, alpha=1)

After the above operation, the regions of different languages will overlap, and we also need to distinguish the overlapping parts.

We distinguish by the following code:

Sns.set_theme (, rc= {"axes.facecolor": (0,0,0,0)} g = sns.FacetGrid (df_filtered, row= "Language", aspect=9, height=1.2) g.map_dataframe (sns.kdeplot, x = "IMDB Score", fill=True, alpha=1) g.map_dataframe (sns.kdeplot, x = "IMDB Score", color='black') g.fig.subplots_adjust (hspace=-.5) g.set_titles (") g.set (yticks= []) g.despine (left=True)

The running results are as follows:

So far, we have implemented our first version of the ridge map, and then we can customize and extend it as needed.

The FacetGrid function is ideal for creating multiple visual legends, and the .map and .map _ dataframe methods allow us to interact with all subgraphs.

The code is as follows:

Sns.set_theme (, rc= {"axes.facecolor": (0,0,0,0), 'axes.linewidth':2}) palette= sns.color_palette ("Set2", 12) g = sns.FacetGrid (df_filtered, palette=palette, row= "Language", hue= "Language", aspect=9, height=1.2) g.map_dataframe (sns.kdeplot, x = "IMDB Score", fill=True, alpha=1) g.map_dataframe (sns.kdeplot, x = "IMDB Score", color='black') def label (x, color) Label): ax = plt.gca () ax.text (0, .2, label, color='black', fontsize=13, ha= "left", va= "center", transform=ax.transAxes) g.map (label, "Language") g.fig.subplots_adjust (hspace=-.5) g.set_titles ("") g.set (yticks= [], xlabel= "IMDB Score") g.despine (left=True) plt.suptitle ('Netflix Originals-IMDB Scores by Language', yaw0.98)

The running results are as follows:

4. Expansion

Finally, we can use the following code to copy the visualization of 喜悦 's Division album cover.

The code is as follows:

Import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltif _ _ name__ = = "_ main__": url = ". / data/pulsar.csv" df = pd.read_csv (url, header=None) df = df.stack (). Reset_index () df.columns = ['idx',' x','y'] sns.set_theme (rc= {"axes.facecolor": (0,0,0,0), 'figure.facecolor':'#000000' 'axes.grid':False}) g = sns.FacetGrid (df, row='idx', aspect=50, height=0.4) # Draw the densities in a few steps g.map (sns.lineplot,' x,'y, clip_on=False, alpha=1, linewidth=1.5) g.map (plt.fill_between,'x, y, color='#000000') g.map (sns.lineplot,'x, y, clip_on=False, color='#ffffff') Lw=2) # Set the subplots to overlap g.fig.subplots_adjust (hspace=-0.95) g.set_titles (") g.set (yticks= [], xticks= [], ylabel=", xlabel= "") g.despine (bottom=True, left=True) plt.savefig ('joy.png', facecolor='#000000')

The running results are as follows:

These are all the contents of the article "how to use python data Visualization Seaborn to draw Ridge Map". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report