美文网首页
【Python | Matplotlib 】基础

【Python | Matplotlib 】基础

作者: 盐果儿 | 来源:发表于2022-08-13 05:39 被阅读0次

What is Matplotlib?

Matplot is a library used to create graphs, charts, and figures. It also provides functions to customize your figures by changing the colors, labels, etc.

1. import library

import matplotlib.pyplot as plt

2. Create a chart

s = pd.Series([1, 2, 3, 4, 5])

# plot the figure

s.plot(kind='bar')

# Save the figure

plt.savefig('plot.png')

3. Line plot

df[df['month'] == 12]['cases'].plot()

# multiple lines

(df[df['month'] == 12])[['cases', 'deaths']].plot()

4. Bar plot

(df.groupby('month')['cases'].sum()).plot(kind="bar")

# multiple columns

# kind="barh" can be used to create a horizontal bar chart.

df = df.groupby('month')[['cases', 'deaths']].sum()

df.plot(kind="bar", stacked=True)

5. Box plot

What is a box plot?

A box plot is used to visualize the distribution of values in a column, basically visualizing the result of the describe() function.

What does each part of the box chart mean?

The green line shows the median value.

The box shows the upper and lower quartiles(25% of the data is greater or less than these values)

The circles show the outliers, while the black lines show the min/max values excluding the outliers.

df[df[”month“]==6]["cases"].plot(kind="box")

6. Histogram

What is histogram?

Histograms show the distribution of data. Visually, histograms are similar to bar charts, however, histograms display frequencies for a group of data rather than an individual data point. Therefore, no spaces are present between the bars.

# You can manually specify the number of bins to use using the bins attribute: plot(kind='hist', bins = 10)

df[df['month']==6]['cases'].plot(kind='hist')

7. Area plot

kind='area' creates an area plot

Area plot are stacked by default, which is why we provided stacked=False explicitly.

8.Scatter plot

A scatter plot is used to show the relationship between two variables.

# x and y columns need to be specified.

s.plot(kind="scatter", x='cases', y='deaths')

9.Pie chart

Pie chart are generally used to show percentage or proportional data.

Pie chart are usually used when you have up to 6 categories.

kind="pie"

10. Plot formatting

# Set legend(图例)

s.plot(kind="line", legend=True)

# By default, pandas select the index name as xlabel, while leaving it empty for ylabel.

plt.xlabel('Name of x axis')

plt.ylabel('Name of y axis')

# Set a plot title

plt.suptitle("Name of title.")

# Set color

s.plot(kind="area", legend=True, stacked=False, color=['#1970E7', '#E73E19'])

相关文章

网友评论

      本文标题:【Python | Matplotlib 】基础

      本文链接:https://www.haomeiwen.com/subject/ppnfgrtx.html