Python Data Visualization __python

Source: Internet
Author: User
Seaborn Library Handbook Translation Introductory Remarks:

Seaborn is actually a more advanced API encapsulation based on Matplotlib, making it easier to draw and, in most cases, using Seaborn to make attractive graphs. I am here to do my best to translate it (the dog has not seen the original computer in English before). ), convenient for everyone to inquire ~ ~ ~ Detailed Introduction can see Seaborn official API and Example Gallery one, style management 1, control picture art style

The ability to visualize data is very important. When you are exploring and analyzing datasets, beautiful, clear charts can help you visualize the data. Data visualization also plays an important role in your communication with your customers, and its visualization helps you to convey the quantitative characteristics of the data itself. Making a compelling chart from this point of view is an indispensable technology.
The Matplotlib library itself has a highly customized capability. But that's why it's hard to learn how to adjust and set parameters to draw beautiful charts. Faced with this problem, Seaborn Library has designed a large number of custom themes and an advanced programming interface to control matplotlib images.
%matplotlib Inline

Import NumPy as NP
import matplotlib as MPL
import matplotlib.pyplot as Plt
import Seaborn as SNS
Np.rando M.seed (SUM (Map (ord, "aesthetics"))

Let's define a function to draw some biased sine waves (. --we will show the effect of different style parameters on this basis.

def sinplot (flip=1):
    x = np.linspace (0, MB) for
    I in range (1, 7):
        plt.plot (x, Np.sin (x + i *. 5) * (7-i ) * Flip)

The following is the default effect of Matplotlib.

Sinplot ()


Use the Set () function to turn it into the default style of Seaborn.

Sns.set ()
Sinplot ()


(Note this in versions of Seaborn prior to 0.8, set () is called on import. On later versions, it must is explicitly invoked).
Seaborn divides the parameters in the matplotlib into two separate sections. Part is responsible for setting the art style of the chart, and the other part is responsible for scaling the various elements in the chart so that it can be easily integrated into different contexts.
We use two pairs of functions as interfaces to manipulate these parameters. Use the Axes_style () and Set_style () function to control the style, and use the Plotting_context and Set_context functions to scale the elements in the table. For both sets of functions, the first one in each group returns a dictionary of parameters, and the second sets the default value for Matplotlib. Seaborn's picture style

There are 5 preset themes in Seaborn: Darkgrid, Whitegrid, dark, white, and ticks. They are suitable for different applications and personal preferences, and the default style is Darkgrid. As demonstrated above, the grid line gives the image the ability to visually express the number of data, and the gray-white line (Darkgrid) clearly distinguishes the image from the grid line. White-bottomed gray lines (Whitegrid) have the same effect, but are more suitable for the mapping of large numbers of data.

Sns.set_style ("Whitegrid")
data = Np.random.normal (size= (6)) + Np.arange (6)/2
Sns.boxplot (data=data);

A lot of times (especially when you want to use charts to give people an impression of data patterns), the grid is less important.

Sns.set_style ("Dark")
Sinplot ()

Sns.set_style ("white")
Sinplot ()

Sns.set_style ("ticks")
Sinplot ()

shift the divisor axis

Both the style white and the ticks can be improved by removing the axis of the top and right axes (not to be changed, of course). This operation cannot be achieved by adjusting the parameters of the matplotlib. But you can do that by calling the Despine () function.

Sinplot ()
sns.despine ()

Moving the axis away from the image can make some images more aesthetically pleasing, and this can also be done through the despine () function when the ticks don t cover the whole range of the axis, the trim parameter wil L limit the range of the surviving spines. (When the axis is not closed, the trim parameter controls the axis to cover the distribution of the data ...). The offset parameter controls the distance between the axis and the image.

F, ax = Plt.subplots ()
sns.violinplot (data=data)
sns.despine (offset=10, trim=true);


You can also select the removed axis by adjusting the parameters in the Despine ().
temporarily set image style

Although it is not difficult to adjust the style , you can still invoke the Axes_style () function in the WITH statement to temporarily set the drawing parameters. This feature allows you to paint in different styles.

With Sns.axes_style ("Darkgrid"):
    plt.subplot (211)
    Sinplot () plt.subplot (
212)
Sinplot (-1)

Overloaded Style parameters

You can customize the Seaborn style by passing in a dictionary to the RC parameters in the Axes_style () and Set_style () functions. Attention. In this way, you can only overload those parameters about the style. (Of course, changes made to the high level set function work on all images)

If you want to view the current parameter settings, you can call without parameters. The parameter settings are returned.

Sns.axes_style ()
{' Axes.axisbelow ': true,
 ' axes.edgecolor ': '. 8 ',
 ' axes.facecolor ': ' White ',
 ' Axes.grid ': true,
 ' Axes.labelcolor ': ', ' Axes.linewidth '
 : 1.0,
 ' figure.facecolor ': ' White ',
 ' font.family ': [ U ' Sans-serif '],
 ' font.sans-serif ': [u ' Arial ',
  u ' dejavu sans ', u '
  liberation sans ',
  u ' bitstream Vera Sans ',
  u ' sans-serif '],
 ' grid.color ': '. 8 ',
 ' grid.linestyle ': U '-',
 ' image.cmap ': U ' rocket ',
 ' Legend.frameon ': False,
 ' legend.numpoints ': 1,
 ' legend.scatterpoints ': 1,
 ' lines.solid_ Capstyle ': U ' round ',
 ' text.color ': '. ',
 ' xtick.color ': '. ',
 ' xtick.direction ': U ' out ',
 ' Xtick.major.size ': 0.0,
 ' xtick.minor.size ': 0.0,
 ' ytick.color ': '. ',
 ' ytick.direction ': U ' out ',
 ' ytick.major.size ': 0.0,
 ' ytick.minor.size ': 0.0}

You can set different versions of the parameters.

Sns.set_style ("Darkgrid", {"Axes.facecolor": ". 9"})
Sinplot ()

scaling elements in a diagram

A set of independent parameters to control the scaling of the image. You can use a line of the same code to control the scaling of the image, so that it fits the different application scenarios.

First we call the SET function to reset the default parameters:

Sns.set ()

There are four preset contexts, sorted by relative size: Paper,notebook,talk and poster. Notebook is the default value

Sns.set_contest ("paper")
Sinplot ()

Sns.set_context ("Talk")
Sinplot ()

Sns.set_context ("poster")
Sinplot ()


Most of what you are know about the style functions should transfer to the context functions.

You can pass the zoom style we mentioned to Set_context and call, of course, you can also pass in a dictionary with parameters to overload each parameter.

You can also scale the font size independently (available only in the top-level set function)

Sns.set_context ("Notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
Sinplot ()

Again (although it may be useless), you can use the method called in the With statement to make a temporary scaling.

The above features can be quickly implemented in the SET function, which also provides a default palette. We will explain this in the next chapter.

Let me summarize. The two basic operations figure styles and scaling plot elements each have Set_style () and Set_context two set methods that can be modified, and two other with+ method to operate temporarily (for a single special requirement). Despine () can operate on an axis. Set function, great harmony of life, superlative setting. The drawing method does not change ~ ~ Establish Sub_plot + Set style + drawing instructions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.