Python语言学习

Python数据可视化(二):折线图绘制

2021-04-23  本文已影响0人  Davey1220

使用matplotlib包绘制折线图

image.png
# 导入所需的python包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 设置绘图格式
plt.style.use('seaborn')
%matplotlib inline
# 创建示例数据
values=np.cumsum(np.random.randn(1000,1))

# 查看示例数据
values[1:10]
array([ 0.24297415,  0.8161543 ,  0.32656029, -0.17953043, -0.80225196,
       -1.27380905, -0.28938979, -3.42407114, -3.58398279])</pre>

绘制基础折线图

# use the plot function
plt.plot(values)

# show the graph
plt.show()
image.png
# 绘制未排序数据的折线图

# import the iris dataset
# 加载示例数据
df = sns.load_dataset('iris')
df.head(10)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
5 5.4 3.9 1.7 0.4 setosa
6 4.6 3.4 1.4 0.3 setosa
7 5.0 3.4 1.5 0.2 setosa
8 4.4 2.9 1.4 0.2 setosa
9 4.9 3.1 1.5 0.1 setosa
# plot
plt.plot( 'sepal_width', 'sepal_length', data=df)

# show the graph
plt.show()
image.png
# 绘制排序后数据的折线图

# 构建示例数据
df=pd.DataFrame({'xvalues': range(1,101), 'yvalues': np.random.randn(100) })
df.head(10)
xvalues yvalues
0 1 0.876885
1 2 0.695569
2 3 0.807841
3 4 0.447100
4 5 -0.186339
5 6 -1.212736
6 7 0.235604
7 8 1.157926
8 9 -0.733519
9 10 0.864461
# plot
plt.plot( 'xvalues', 'yvalues', data=df)

# show the graph
plt.show()
image.png

自定义线的颜色

# 构建示例数据
df=pd.DataFrame({'x_values': range(1,11), 'y_values': np.random.randn(10) })

# Draw plot
# 设置color参数自定义线的颜色
plt.plot( 'x_values', 'y_values', data=df, color='skyblue')
plt.show()
image.png
# Draw line chart by modifiying transparency of the line
# 设置alpha参数更改线的透明度
plt.plot( 'x_values', 'y_values', data=df, color='red', alpha=0.3)

# Show plot
plt.show()
image.png

自定义线的类型

# Draw line chart with dashed line
# 设置linestyle参数自定义闲的类型
plt.plot( 'x_values', 'y_values', data=df, linestyle='dashed')

# Show graph
plt.show()
image.png
# 查看不同的线型
plt.plot( [1,1.1,1,1.1,1], linestyle='-' , linewidth=4)
plt.text(1.5, 1.3, "linestyle = '-' ", horizontalalignment='left', size='medium', color='C0', weight='semibold')

plt.plot( [2,2.1,2,2.1,2], linestyle='--' , linewidth=4 )
plt.text(1.5, 2.3, "linestyle = '--' ", horizontalalignment='left', size='medium', color='C1', weight='semibold')

plt.plot( [3,3.1,3,3.1,3], linestyle='-.' , linewidth=4 )
plt.text(1.5, 3.3, "linestyle = '-.' ", horizontalalignment='left', size='medium', color='C2', weight='semibold')

plt.plot( [4,4.1,4,4.1,4], linestyle=':' , linewidth=4 )
plt.text(1.5, 4.3, "linestyle = ':' ", horizontalalignment='left', size='medium', color='C3', weight='semibold')

plt.axis('off')
plt.show()
image.png

自定义线的宽度

# Modify line width of the graph
# 设置linewidth参数自定义线的宽度
plt.plot( 'x_values', 'y_values', data=df, linewidth=22)

# Show graph
plt.show()
image.png

绘制多线折线图

# Data
# 构建示例数据
df=pd.DataFrame({'x_values': range(1,11), 'y1_values': np.random.randn(10), 'y2_values': np.random.randn(10)+range(1,11), 'y3_values': np.random.randn(10)+range(11,21) })

df.head(10)
x_values y1_values y2_values y3_values
0 1 1.067931 1.387085 10.330824
1 2 -0.539553 1.718083 12.094820
2 3 0.031352 2.526630 11.560346
3 4 -0.693288 3.364125 12.002817
4 5 -0.176465 6.821868 13.932456
5 6 1.109269 5.875778 15.407165
6 7 -0.750049 5.325365 18.765016
7 8 2.084154 8.578474 18.401151
8 9 0.418775 9.524832 19.062925
9 10 2.051114 9.682992 18.867805
# multiple line plots
plt.plot( 'x_values', 'y1_values', data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'x_values', 'y2_values', data=df, marker='', color='olive', linewidth=2)
plt.plot( 'x_values', 'y3_values', data=df, marker='', color='red', linewidth=3, linestyle='dashed', label="toto")

# show legend
plt.legend()

# show graph
plt.show()
image.png

高亮特定折线

# Change the style of plot
plt.style.use('seaborn-darkgrid')

# set figure size
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Make a data frame
df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21), 'y4': np.random.randn(10)+range(6,16), 'y5': np.random.randn(10)+range(4,14)+(0,0,0,0,0,0,0,-3,-8,-6), 'y6': np.random.randn(10)+range(2,12), 'y7': np.random.randn(10)+range(5,15), 'y8': np.random.randn(10)+range(4,14) })
df.head(10)
x y1 y2 y3 y4 y5 y6 y7 y8
0 1 -0.687924 2.584799 11.480629 7.847702 3.962812 2.817641 4.716701 5.037785
1 2 -1.190644 2.509128 10.639934 6.096679 6.023990 3.593341 5.784641 3.675451
2 3 -0.355472 1.879478 13.651546 8.620174 6.008777 2.712259 6.934400 6.219872
3 4 0.978479 4.565185 12.986859 9.257281 7.801419 4.372574 8.886010 7.617974
4 5 -0.137379 6.454554 13.882441 8.835127 6.986779 7.970914 8.343824 9.527383
5 6 1.479008 4.857621 16.752558 11.290006 8.520126 6.507897 10.274646 7.339675
6 7 -0.862117 4.919770 18.905680 13.265195 10.892788 7.208971 10.757919 8.773337
7 8 -0.787397 9.109201 17.460583 12.353985 7.479977 8.964906 12.300782 11.636784
8 9 0.815745 8.755666 18.685683 12.728094 4.306399 10.714981 13.400910 11.165933
9 10 0.443319 9.686925 20.563202 16.168934 6.827163 10.188284 15.023878 13.611045
# 绘制多条折线图
# plot multiple lines
for column in df.drop('x', axis=1):
    plt.plot(df['x'], df[column], marker='', color='grey', linewidth=1, alpha=0.4)

# 高亮其中的一条折线
# Now re do the interesting curve, but biger with distinct color
plt.plot(df['x'], df['y5'], marker='', color='orange', linewidth=4, alpha=0.7)

# Change x axis limit
plt.xlim(0,12)

# 给每条折线添加注释信息
# Let's annotate the plot
num=0
for i in df.values[9][1:]:
    num+=1
    name=list(df)[num]
    if name != 'y5':
        plt.text(10.2, i, name, horizontalalignment='left', size='small', color='gray')

# And add a special annotation for the group we are interested in
plt.text(10.2, df.y5.tail(1), 'Mr Orange', horizontalalignment='left', size='small', color='orange')

# 添加标题和坐标轴
# Add titles
plt.title("Evolution of Mr Orange vs other students", loc='left', fontsize=12, fontweight=0, color='orange')
plt.xlabel("Time")
plt.ylabel("Score")

# Show the graph
plt.show()
image.png

绘制分面折线图

# Initialize the figure style
plt.style.use('seaborn-darkgrid')

# create a color palette
palette = plt.get_cmap('Set1')
# 构建示例数据
# Make a data frame
df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21), 'y4': np.random.randn(10)+range(6,16), 'y5': np.random.randn(10)+range(4,14)+(0,0,0,0,0,0,0,-3,-8,-6), 'y6': np.random.randn(10)+range(2,12), 'y7': np.random.randn(10)+range(5,15), 'y8': np.random.randn(10)+range(4,14), 'y9': np.random.randn(10)+range(4,14) })
df.head(10)
x y1 y2 y3 y4 y5 y6 y7 y8 y9
0 1 0.705547 -0.185696 13.079950 4.935681 3.558236 1.987303 3.777348 4.319105 3.013749
1 2 1.207474 3.093301 12.163533 5.769417 5.644947 3.789781 7.864374 4.913551 5.306410
2 3 -1.078666 3.198830 12.653673 8.683952 7.618591 3.482401 7.515903 5.254345 5.381230
3 4 -1.171316 4.410335 12.971149 10.613208 6.895600 4.867909 8.421857 7.340826 7.443260
4 5 1.270850 5.748285 16.414187 9.581892 7.723042 6.661644 8.781812 8.272855 7.745660
5 6 -0.302443 6.361198 16.327518 9.788331 8.850974 6.881167 9.072248 10.308536 7.794249
6 7 -0.475574 7.620870 17.139569 13.205853 11.437107 6.388262 11.043212 9.883755 10.771894
7 8 0.363418 6.782794 17.710851 13.262601 9.111311 9.904678 13.348669 10.125153 10.947822
8 9 3.034346 10.164225 19.036592 14.160345 4.305579 8.897607 12.429754 11.348469 11.271929
9 10 1.141996 9.590279 19.169931 16.149196 6.473655 10.561281 14.805126 12.603702 11.674404
# multiple line plot
num=0
for column in df.drop('x', axis=1):
    num+=1

    # Find the right spot on the plot
    plt.subplot(3,3, num)

    # Plot the lineplot
    plt.plot(df['x'], df[column], marker='', color=palette(num), linewidth=1.9, alpha=0.9, label=column)

    # Same limits for every chart
    plt.xlim(0,10)
    plt.ylim(-2,22)

    # Not ticks everywhere
    if num in range(7) :
        plt.tick_params(labelbottom='off')
    if num not in [1,4,7] :
        plt.tick_params(labelleft='off')

    # Add title
    plt.title(column, loc='center', fontsize=12, fontweight=0, color=palette(num) )

# general title
plt.suptitle("How the 9 students improved\nthese past few days?", fontsize=13, fontweight=0, color='black', style='italic', y=1.02)

# Axis titles
plt.text(5, 0, 'Time', ha='center', va='center')
plt.text(0, 10, 'Note', ha='center', va='center', rotation='vertical')

# Show the graph
plt.show()
image.png
# multiple line plot
num=0
for column in df.drop('x', axis=1):
    num+=1

    # Find the right spot on the plot
    plt.subplot(3,3, num)

    # plot every group, but discrete
    for v in df.drop('x', axis=1):
        plt.plot(df['x'], df[v], marker='', color='grey', linewidth=1, alpha=0.3)

    # Plot the lineplot
    plt.plot(df['x'], df[column], marker='', color=palette(num), linewidth=3, alpha=0.9, label=column)

    # Same limits for every chart
    plt.xlim(0,10)
    plt.ylim(-2,22)

    # Not ticks everywhere
    if num in range(7) :
        plt.tick_params(labelbottom='off')
    if num not in [1,4,7] :
        plt.tick_params(labelleft='off')

    # Add title
    plt.title(column, loc='center', fontsize=12, fontweight=0, color=palette(num) )

# general title
plt.suptitle("How the 9 students improved\nthese past few days?", fontsize=13, fontweight=0, color='black', style='italic', y=1.02)

# Axis titles
plt.text(5, 0, 'Time', ha='center', va='center')
plt.text(0, 10, 'Note', ha='center', va='center', rotation='vertical')

# Show the graph
plt.show()
image.png

参考来源:https://www.python-graph-gallery.com/line-chart/

上一篇 下一篇

猜你喜欢

热点阅读