Data Visualization-matplotlib Simple Introduction (i)

Source: Internet
Author: User
Tags ggplot

Content source for this section: Https://www.dataquest.io/mission/10/plotting-basics

Data source for this section: Https://archive.ics.uci.edu/ml/datasets/Forest+Fires

Raw data presentation (this table records a fire in a park, X and y represent the coordinates, area is the burnt surface)

Import= pandas.read_csv ('forest_fires.csv')print( Forest_fires.head (5))

When using the Matplotlib library, it will be abbreviated as PLT by default.

import Matplotlib.pyplot as Plt

A drawing process is divided into three steps:

1. Initializing drawing data

2. Drawing

3. Show the diagram

Let's snack.

Use the scatter () method to make a scatter plot, the first parameter as the x-axis, the second parameter as the y-axis, and note that two parameters can only be List data or series

# use list data as an axis Import  = [600,150,200,300,200,100,125,180= [60,65,73,70,65,58,66,67]plt.scatter (height, weight) Plt.show ()

# use series as an axis # using wind data as the x-axis, the burnt-out area is used as the y-axis, making their scatter plots . plt.scatter (forest_fires["Wind"), forest_fires[" Area "]) plt.show ()

You can notice that the above two graphs have no graph title, and there is no horizontal and vertical text description, you can use several functions to add the corresponding information:

    • Title ()--adding a graph to a table title
    • Xlabel ()--Add text description information for x-axis
    • Ylabel ()--Add text description information for y-axis
Plt.scatter (forest_fires['wind'), forest_fires['area'  ]) plt.title ('wind speedvs. Fire area') Plt.xlabel ('wind speed when fire started') Plt.ylabel ('areaconsumedby fire'  ) plt.show ()

Line chart

The line chart is plotted using the plot () function, and the parameter requirements are the same as the scatter plot above, just one example.

# use list data as an axis  = [5, ten, +, +, +, += [],----------]plt.plot (age, height) plt.title ('  Age vs. Height') Plt.xlabel ('age') Plt.ylabel ( ' Height ' ) plt.show ()

Bar chart

Use the bar () function to draw the vertical type of bar chart, the parameters require the same as above, just to give an example

# now we're going to count the burned area by month. # make a Perspective chart to calculate the burnt area of each month  = forest_fires.pivot_table (index="month", values=" Area ", Aggfunc=numpy.sum)

Plt.bar (range (len Area_by_month), Area_by_month) plt.title ('month vsarea' ) Plt.xlabel ('month') Plt.ylabel ('area')  ) plt.show ()

It is important to note that the x-axis corresponds to the number does not represent the month, because the data in the Area_by_month is unordered

Use the Barh () function to draw a horizontal bar chart, note that the biggest difference from the bar () function is that the x-axis and y-axis are reversed.

Plt.barh (range (len Area_by_month), Area_by_month) plt.title ('month vsarea' ) Plt.xlabel ('area') Plt.ylabel ('month')  ) plt.show ()

You can see that the x-axis and y-axis are reversed.

Use a different drawing theme

You can choose to use a different drawing theme and use the Style.use () function to

# use two themes to compare plt.style.use ('fivethirtyeight') plt.plot (forest_fires[' Rain '], forest_fires['area') plt.show ()

Plt.style.use ('ggplot') plt.plot (forest_fires['rain') ], forest_fires['area') plt.show ()

The following topics are commonly used:

Fivethirtyeight,ggplot,dark_background,bmh

Data Visualization-matplotlib Simple Introduction (i)

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.