Entradas


The swarm plot is identical to the strip plot, except that the edges are modified in such a way that they don’t intersect to one another, which helps to effectively illustrate the visualization of the data. A swarm graph is created alone. But it’s preferred to utilize it in conjunction with a box since the corresponding titles are used to label the dimensions. Let’s draw the swarm maps with the help of the swarmplot() function.

Example no.1:

Here, we create a categorized probability plot with dots that do not overlap. So, we utilize the swarmplot() method to make a plot containing the discrete values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

df = pd.DataFrame({«Quantity»: [15,26,17,18,15,36,27,18,25,16,17,28,15,16,17,28],

«Price»:[1900,1000,1500,1600,1300,1400,1500,1800,1100,1200,1400,1500,1600,1700,1800,1900],

                   «Month» : [2,3,2,3,2,3,2,3,4,4,4,5,5,5,4,3],
                   “Merchandise«:[‘X’,’X’,’X’,’X’,’Z’,’Z’,’Z’,’Z’,
                              ‘Y’,’Y’,’Y’,’Y’,’X’,’X’,’Z’,’Z’]})

sns.swarmplot(data = df, y = «Price«, x = «Quantity«)

plt.show()


At the beginning of the code, we integrate the packages Pandas as pd, matplotlib.pyplot as plt, and Seaborn as sns. Next, we specify the set of data with the help of the DataFrame() method. This function is associated with the Pandas module. We create four different arrays. The first array contains the quantity of the products that have been sold out. The second array shows the rates of the products. The third array holds the record of the months. The last array has data on the product names.

In the next step, we want to draw the swarm graph, so we call the swarmplot() function. In the end, we employ the show() function of the matplotlib.pyplot library.

Example no.2:

We utilize the hue argument within the swarmplot() method and split the segments for the multiple products in this instance.  By specifying the value of the “dodge” parameter to True, we segregate the items. We might also pass some additional parameters by using the swarmplot() method. The size parameter is used to adjust the “size” of the elements.

With the help of the “palette” attribute, we change the color scheme for distinct groups. The “linewidth” option provides a boundary to the defined width of dots. Let’s apply all the previously parameters in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

df = pd.DataFrame({«Quantity»: [15,26,17,18,15,36,27,18,25,16,17,28,15,16,17,28],

«Price»:[1900,1000,1500,1600,1300,1400,1500,1800,1100,1200,1400,1500,1600,1700,1800,1900],

                   «Month» : [2,3,2,3,2,3,2,3,4,4,4,5,5,5,4,3],
                   “Product«:[‘X’,’X’,’X’,’X’,’Z’,’Z’,’Z’,’Z’,
                              ‘Y’,’Y’,’Y’,’Y’,’X’,’X’,’Z’,’Z’]})

sns.swarmplot(data = df, y = «Price«, x = «Quantity«, hue=»Product», dodge = True,
              linewidth = 3.5 , palette=»Set2″, size  = 14)

plt.show()


First of all, we integrate the required header files. The Pandas library is integrated as pd, matplotlib.pyplot is integrated as plt, and Seaborn is integrated as sns. The DataFrame() function is used to provide the data set. The Pandas package is linked to this method. We make four unique arrays. The number of sold-out items is represented in the first array. The pricing of the commodities is displayed in the second array. The data of the months is kept in the third array. The titles of the merchandise are stored in the last array.

Now, the swarm figure is drawn, thus we utilize the swarmplot() method. The data set, x- and y-axis labels, hue, dodge, linewidth, palette, and size are all arguments for this method. The value of the “hue” is the product. The “linewidth” is 3.5. The “palette” is set2. And the “size” is 14. We terminate the code by illustrating the resultant plot, so we apply the show() method.

Example no.3:

Every dimension of the “hue” parameter is represented by a designated area on the statistical category plane. We configure the “dodge” to True while employing the “hue” parameter and it isolates the items for multiple hue variations. The “palette” parameter is used to depict the various shades of the hue attribute.

1
2
3
4
5
6
7
8
9
10
11
12

import seaborn

import matplotlib.pyplot as plt

seaborn.set(style=«whitegrid»)

tips = seaborn.load_dataset(«tips»)

seaborn.swarmplot(x=«day», y=«total_bill», hue=«smoker»,
                data=tips, palette=«Set2», dodge=True)

plt.show()

After including the Seaborn and matplotlib.pyplot libraries, we call the set() function of the Seaborn package. We pass the style as the parameter to this function. We give the “whitegrid” value to the style parameter. It shows the backdrop color of the graph.

Now, we obtain the built-in data frame, so we use the load_dataset() function. This function is taken from the Seaborn header file and it contains the “tips” as its argument. Next, we utilize the swarmplot() method to create the swarm chart. Here, we specify the title of both axes, the value of hue, data, palette, and dodge as the parameters of the function.  The x-axis displays the record of the days whereas the y-axis shows the record of the total_bill. To represent the final graph, we call the show() method. The matplotlib.pyplot module contains this functionality.

Example no.4:

With the help of the “marker” attribute as well as the “alpha” argument, we draw the massive points and diverse styles. We employ the “alpha” attribute to control the data value’s visibility. And apply the “marker” argument for the indicator to modify the set of data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

import seaborn

import matplotlib.pyplot as plt

seaborn.set(style=«whitegrid»)

 

tips = seaborn.load_dataset(«tips»)

 

seaborn.swarmplot(x=«day», y=«total_bill», hue=«smoker»,

                   data=tips, palette=«Set2», size=30, marker=«*»,
                   edgecolor=«black», alpha=.35)

plt.show()


Here, we introduce the Seaborn and matplotlib.pyplot frameworks. The set() method of the Seaborn component is used. The style is supplied as an argument for this method. We provide the style variable with the “whitegrid” value. It displays the visual appearance of the chart.

We intend to get the built-in data frame, so we call the load dataset() method. This method is obtained from the Seaborn template and has the “tips” parameter. The swarm figure is then created with the help of the swarmplot() technique. The function’s inputs are the caption of both axes, hue value, data, palette, size of the marker, shape of the marker, edgecolor, and alpha value.

The x-axis demonstrates the data of the days, while the y-axis indicates the total bill’s record. The markers of the shape ‘*’ with the size 20 are found in this swarm map. The show() function of matplotlib.pyplot is used to depict the ultimate graph.

Conclusion

We discussed the various techniques for plotting the swarm plot in this article. Swarm maps are a form of scatter graph that are applied to display categorical data. It prevents the elements from overlapping. We can utilize the swarmplot() method to draw these plots. Whenever the sample frame is huge, we cannot use this sort of graph.



Source link