Python Visualization Using Matplotlib and Seaborn

Source: Internet
Author: User
Keywords seaborn seaborn tutorial seaborn python
Seaborn and Matplotlib are the two most powerful visualization libraries in Python. Seaborn's default theme is amazing, and Matplotlib can create features for users through its multiple categories.
Python provides a variety of packages for drawing data. This tutorial uses two packages to demonstrate Python's drawing capabilities:
· Matplotlib
· Seaborn
Matplotlib
importmatplotlib.pyplot  as plt%matplotlib inlineimport numpy as np
In the code block above, adjust the pyplot module to PLT format and import it into the matplotliib library. This simplifies the execution of commands, as you will see in this tutorial. Pyplot contains a series of commands needed to create and edit a plot. Run% matplotlibinline at the same time during the operation, the code block will be automatically displayed under the drawing, otherwise the user needs to input each time he draws plt.show () to create a new diagram. This feature is unique to jupyter notebook / IPython. Matplotlib has a custom block structure, which makes it more advanced than other drawing libraries. Next let's see how to generate a scatter diagram using matploblib.
Tip: when using Matplotlib, text output doesn't have a visual appeal. To solve this problem, we will add a semicolon ';' at the end of each line of code when executing the code block to generate a picture.
The database we use is the shared bicycle data set in UCI machine learning library.
Matplotlib: scatter
Scatter diagram is one of the most influential, informative and functional diagrams. It can pass information directly to users without much extra work (as shown in the figure below).
·  plt.scatter () take the data on the input scatter diagram as the initial parameter, in which temp refers to the x-axis and CNT refers to the y-axis.
·C refers to the color of different data points. Enter a string "season", which also represents a column in the data frame. Different colors correspond to different seasons. This method uses visual method to group data, which is very simple and convenient.
plt.scatter ('temp','cnt', data=day, c='season') plt.xlabel ('NormalizedTemperature', fontsize='large') plt.ylabel ('Countof Total Bike Rentals', fontsize='large');
The information on the figure shows:
·At some points in time, more than 8000 bicycles were leased.
·The standard temperature is over 0.8.
·Bicycle rental volume has nothing to do with temperature or season.
·There is a main line relationship between bicycle rental volume and standard temperature.
This chart does contain a lot of information, but it does not produce a legend, so it is difficult to decipher the seasonal data of each group. This is because Matplotlib is unable to make a legend from the plot when drawing with a scatter plot. In the next section, we'll see how the graph hides information and even misleads readers.
Take a look at the thoroughly edited drawing. The purpose of the diagram is to draw legends according to different groups and interpret them.
plt.rcParams ] figure.figsize ']= [15, 10]fontdict={'fontsize':18,'weight' : 'bold', 'horizontalalignment': 'center'}fontdictx={'fontsize':18, 'weight' : 'bold', 'horizontalalignment': 'center'}fontdicty={'fontsize':16, 'weight' : 'bold', 'verticalalignment': 'baseline', 'horizontalalignment': 'center'}spring = plt.scatter ('temp', 'cnt',  data=day[day['season']==1], marker='o',color='green')summer = plt.scatter ('temp', 'cnt', data=day[day['season']==2], marker='o', color='orange')autumn = plt.scatter ('temp', 'cnt', data=day[day['season']==3], marker='o',color='brown')winter = plt.scatter ('temp', 'cnt', data=day[day['season']==4], marker='o',color='blue') plt.legend (handles=(spring, summer,autumn,winter), labels=('Spring', 'Summer','Fall/Autumn', 'Winter'), title="Season",title_ fontsize=16, scatterpoints=1, bbox_ To_ anchor=(1, 0.7), loc=2,borderaxespad=1., ncol=1, fontsize=14) plt.title ('BikeRentals at Different Temperatures\nBy Season', fontdict=fontdict,color="black") plt.xlabel ("Normalizedtemperature", fontdict=fontdictx) plt.ylabel ("Countof Total Rental Bikes", fontdict=fontdicty);
·  plt.rcParams ] figure.figsize '] = [15, 10] defines the size of the whole picture. This code corresponds to a picture 15 cm long * 10 cm wide.
·Fontdict is a dictionary with many parameters, which can be used to mark different axes. The title is fontdict, the X axis is fontdictx, and the Y axis is fontdicty.
·Now there are four plt.scatter () function, corresponding to four different seasons, which appears again in the data parameters, which have been sub centralized to correspond to different single seasons. "O" represents different markers and color parameters, which visually shows the location of data points and their colors.
·At plt.legend The input parameters in () form a legend. The first two parameters are the handle: the legend and label will show the real scatter diagram, and the name corresponding to each diagram will also appear in the legend. The marks with different sizes on the graph represent scatter points, which are finally combined into a scatter chart. bbox_ To_ anchor=(1, 0.7), loc=2, borderaxespad=1。 These three parameters are used in series to compare the legend position. Click the link at the beginning of this sentence to find out the nature of these parameters.
Now you can find more hidden information by distinguishing between different seasons. But even if additional layers have been added, scatter can still hide information and cause misunderstanding.
The scatter chart:
·There are duplicates in the data.
·The distribution is chaotic.
·There is no significant difference in bicycle rental between different seasons.
·Hidden information, such as: when the temperature rises in spring and summer, bicycle rental will increase.
·Indicates that the bicycle rental volume is in direct proportion to the temperature.
·It is not clear which season the lowest temperature occurs.
Subgraph
Creating subgraphs is probably one of the most attractive and professional charting techniques in the industry. When a single graph has too much information, it is difficult for people to evaluate, so subgraphs become very important.
Faceting is drawing several different graphs on the same axis. Faceting is one of the most functional technologies in data visualization. Faceting can display information from multiple perspectives, or reveal hidden content.
·As mentioned before, plt.figure () will be used to create a new drawing canvas. It will eventually be saved as a picture format.
·Repeat operation fig.add_ The subplot () code is 4 times, corresponding to the subplots of four seasons. Its parameters correspond to nrows, ncols and index. For example, Axl corresponds to the first plot on the chart (the index starts from 1 in the upper left corner and increases to the right).
·The remaining function calls can be self explanatory or overridden.

Related Article

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.