in pythonData visualizationIn the field, matplotlib has always been the most widely used visualization library. However, the complexity of its operation has always plagued users. To solve this problem,seabornIt came into being and has received a lot of attention for its simplified operation process. Be that as it may,seabornThere are still some deficiencies in the way they are used. After up to 10 years of polishing,seabornThe team is in 0The 12th version of the update proposes a completely new mode of operation. After trying it out, I've been completely blown away by this new model and don't want to go back to the old way of using it. In this article, we'll take a look at the new versionseaborn。This time, it is:seaborn0.13 version.
In the new mode,seabornA type of work called "Object" was introducedinterface"mode of operation, via importseaborn.objects module. Before we begin, we need to import data, and here I've used a piece of coffee sales data as an example. Before starting the drawing, we need to do some configuration to ensure that the Chinese in the chart does not appear garbled. Next, we can happily start drawing.
Let's start by looking at the relationship between profit and sales. Since these twovariablesIt's all numerical and we can use itScatter plotto represent. In the new pattern, following the specification of visual graph syntax, we first use soplot() to declare the data and mapping, and then use the add() method to chain and add a new graph each time the add() method is called. In the add() method, the first argument specifies the type of graph you want, and here we use sodot() to represent "point". In this way, we can easily draw outScatter plot
Now, we hope to be on the topScatter plotAdd a fitting polyline. In the new pattern, each add() method call is equivalent to adding a new chart or layer, the first parameter specifies the pattern type, and the subsequent parameters are used to transform the data. We can use sopolyfit() to perform the fitting calculation. Of course, the calculation method can be customized. With this method, we can easily add a fitting polyline to the chart. The new model may seem a little complicated, but then you'll find its charm.
Next, we don't just want to drawScatter plot, and also hope to classify and display according to different regions. Categorizing by region means that we need to assign a different color to each region. In the new mode, it's very simple, just do the color mapping in **. In addition to color, there are many other properties that can be mapped, such as the fact that we can adjust the size of points based on gross profit. In this way, we can easily pair:Scatter plotConduct a variety of presentations. Compared with the old model, the advantages of the new model are obvious, and it greatly reduces our burden.
Continue with examples. We can use the new model to easily plot out the sales of the regionHistogram。However, when executing the ** above, you may find that the drawing takes a very long time. At the same time, the drawn charts are a bit strange. The reason for this is that we don't specify the aggregation method in the add() method call. Therefore, by default, the program draws each line as a bar. Solving this problem is simple, just specify it in the add() methodHistogramcan be aggregated. By soThe first argument of the agg() method, we can specify the aggregation method, which is the default'mean', which can also be passed into othersAggregate functionsor a custom function. This is related to:pandasThe principle of the agg() method is the same.
Next, inHistogramWe want to segment the display by market category. That is, color mapping according to different market categories. However, when we run the ** above, something seems to be wrong with the results. Why do small markets sell more than major markets? Let's analyze the process to understand the result. The data reflects the three fields of x, y, and color, so that they are plottedHistogram, you need to group the x and color fields and aggregate the y columns evenly. By soagg() method, we can get the following aggregation result. I believe that after this explanation, you have fully understood the mechanics of the new mode, and it is simply too easy to implement! You can look at some more examples to deepen your impression.
When using the new pattern, we can add an infinite number of data handling functions to the add() methodseabornSome commonly used data manipulation methods are also provided for us to choose from. Just choose the appropriate method according to your needs. Now it's time to make the most of these methods, they are really very convenient and practical. Come and try it, you will feel the charm of it. Don't forget to like, favorite, and follow, this is the biggest motivation for my creation.
In this article, we covered:seabornA new mode of operation for libraries - objectsinterface。By introducing objectsinterfaceseabornGreatly simplifiedData visualizationprocess. In the new pattern, we first build the graph by declaring data and mappings, and then add layers step by step by chaining the add() method. Each layer can specify a different pattern type and transform the data through data processing functions. At the same time,seabornIt provides a wealth of data manipulation methods, which can be freely selected according to needs. In this way, we can easily draw a variety of charts that show multiple pieces of dataDimensions
Through the study and practice of new models, we have not only mastered:seabornand a deep understanding of the principles behind it. Compared with the traditional model of one function and one chart, the new model greatly simplifies our use burden and improves the efficiency and flexibility of visualization. Through the introduction of this article, I hope that readers will be rightseabornhas a deeper understanding of the new model and is able to get started quickly.