使用matplotlib包绘制面积图
# libraries
import numpy as np
import matplotlib.pyplot as plt
# Create data
x=range(1,6)
y=[1,4,6,8,4]
# Area plot
# 绘制基础面积图
plt.fill_between(x, y)
# Show the graph
plt.show()
image.png
# create data
x=range(1,15)
y=[1,4,6,8,4,5,3,2,4,1,5,6,8,7]
# Change the color and its transparency
plt.fill_between( x, y, color="skyblue", alpha=0.4)
# Show the graph
plt.show()
image.png
# Same, but add a stronger line on top (edge)
plt.fill_between( x, y, color="skyblue", alpha=0.2)
plt.plot(x, y, color="Slateblue", alpha=0.6)
# See the line plot function to learn how to customize the plt.plot function
# Show the graph
plt.show()
image.png
# Change the style of plot
plt.style.use('seaborn-darkgrid')
# Make the same graph
plt.fill_between( x, y, color="skyblue", alpha=0.3)
plt.plot(x, y, color="red")
# Add titles
plt.title("An area chart", loc="left")
plt.xlabel("Value of X")
plt.ylabel("Value of Y")
# Show the graph
plt.show()
image.png
使用seaborn包绘制面积图
# libraries
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create a dataset
my_count=["France","Australia","Japan","USA","Germany","Congo","China","England","Spain","Greece","Marocco","South Africa","Indonesia","Peru","Chili","Brazil"]
df = pd.DataFrame({
"country":np.repeat(my_count, 10),
"years":list(range(2000, 2010)) * 16,
"value":np.random.rand(160)
})
df.head()
|
country |
years |
value |
0 |
France |
2000 |
0.622723 |
1 |
France |
2001 |
0.665459 |
2 |
France |
2002 |
0.048021 |
3 |
France |
2003 |
0.679705 |
4 |
France |
2004 |
0.135426 |
# Create a grid : initialize it
g = sns.FacetGrid(df, col='country', hue='country', col_wrap=4, )
# Add the line over the area with the plot function
g = g.map(plt.plot, 'years', 'value')
# Fill the area with fill_between
g = g.map(plt.fill_between, 'years', 'value', alpha=0.2).set_titles("{col_name} country")
# Control the title of each facet
g = g.set_titles("{col_name}")
# Add a title for the whole plot
plt.subplots_adjust(top=0.92)
g = g.fig.suptitle('Evolution of the value of stuff in 16 countries')
# Show the graph
plt.show()
image.png
绘图堆叠面积图
# libraries
import numpy as np
import matplotlib.pyplot as plt
# Your x and y axis
x=range(1,6)
y=[ [1,4,6,8,9], [2,2,7,10,12], [2,8,5,10,6] ]
# Basic stacked area chart.
plt.stackplot(x,y, labels=['A','B','C'])
plt.legend(loc='upper left')
plt.show()
image.png
# Your x and y axis
x = range(1,6)
y = [ [10,4,6,5,3], [12,2,7,10,1], [8,18,5,7,6] ]
# use a known color palette
pal = sns.color_palette("Set1")
# 设置colors=pal参数自定义颜色画板
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4 )
plt.legend(loc='upper right')
plt.show()
image.png
# Make data
data = pd.DataFrame({ 'group_A':[1,4,6,8,9], 'group_B':[2,24,7,10,12], 'group_C':[2,8,5,10,6], }, index=range(1,6))
# We need to transform the data from raw data to percentage (fraction)
data_perc = data.divide(data.sum(axis=1), axis=0)
data_perc.head()
|
group_A |
group_B |
group_C |
1 |
0.200000 |
0.400000 |
0.400000 |
2 |
0.111111 |
0.666667 |
0.222222 |
3 |
0.333333 |
0.388889 |
0.277778 |
4 |
0.285714 |
0.357143 |
0.357143 |
5 |
0.333333 |
0.444444 |
0.222222 |
# Make the plot
plt.stackplot(range(1,6), data_perc["group_A"], data_perc["group_B"], data_perc["group_C"], labels=['A','B','C'])
plt.legend(loc='upper left')
plt.margins(0,0)
plt.title('100 % stacked area chart')
plt.show()
image.png
# 使用panda包绘制堆叠面积图
# Dataset
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
# plot
df.plot.area()
# show the graph
plt.show()
image.png
网友评论