折线图或折线图是一种将信息显示为一系列数据点的图表,这些数据点由直线段连接。 它与散点图相似,不同之处在于测量点是有序的(通常是按其x轴值排序)并与直线段相连。
(1)Basic lineplot基础图形
代码语言:javascript复制# In[*]
# libraries
import matplotlib.pyplot as plt
import numpy as np
# create data
values=np.cumsum(np.random.randn(1000,1))
# use the plot function
plt.plot(values)
(2)Seaborn customization使用seaborn
代码语言:javascript复制# libraries
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# create data
values=np.cumsum(np.random.randn(1000,1))
# use the plot function
plt.plot(values)
(3)设置数据和绘图
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
df=pd.DataFrame({'xvalues': range(1,101), 'yvalues': np.random.randn(100) })
# plot
plt.plot( 'xvalues', 'yvalues', data=df)
plt.show()
(4)设置线条颜色
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df=pd.DataFrame({'x': range(1,11), 'y': np.random.randn(10) })
plt.plot( 'x', 'y', data=df, color='skyblue')
plt.show()
(5)设置线条透明度
代码语言:javascript复制plt.plot( 'x', 'y', data=df, color='skyblue', alpha=0.3)
plt.show()
(6)设置线条f风格
代码语言:javascript复制plt.plot( 'x', 'y', data=df, linestyle='dashed')
plt.show()
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()
(7)设置线条宽度
代码语言:javascript复制plt.plot( 'x', 'y', data=df, linewidth=22)
plt.show()
(8)绘制多个线条
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Data
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) })
# multiple line plot
plt.plot( 'x', 'y1', data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'x', 'y2', data=df, marker='', color='olive', linewidth=2)
plt.plot( 'x', 'y3', data=df, marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")
plt.legend()
(9)强调感兴趣线条
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 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) })
#plt.style.use('fivethirtyeight')
plt.style.use('seaborn-darkgrid')
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# multiple line plot
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 xlim
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='grey')
# 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")