经常重复使用的绘图代码
使用SciencePlots画论文配图可见:传送门
折线图
import matplotlib.pyplot as plt
import matplotlib as mpl
# 中文和负号的正常显示
mpl.rcParams['font.sans-serif'] = ['Times New Roman']
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
#自定义刻度和标签
times=data1['start_time_noday'].tolist()
# 分时间区间,保证最后一位纳入标签
ticks=list(range(0,len(times),2))
if ticks[-1]!=len(times)-1:
ticks.append(len(times)-1)
labels=[times[i] for i in ticks]
##绘图
fig= plt.figure(figsize=(8, 4),dpi=100)
# 设置图形的显示风格
plt.style.use('ggplot')
ax1 = fig.add_subplot(111)
ax1.plot(data1['order_id'],'-*',linewidth=1.5,label='非雨天工作日')
ax1.plot(data2['order_id'],'-o',linewidth=1.5,label='非雨天周末')
ax1.plot(data3['order_id'],'-v',linewidth=1.5,label='雨天工作日')
ax1.plot(data4['order_id'],'-^',linewidth=1.5,label='雨天周末')
ax1.legend(loc='upper right', frameon=False,fontsize = 10)
ax1.set_xlabel('时间',fontsize =10)
ax1.set_ylabel('订单量',fontsize =10)
ax1.set(xlim=[0,len(times)-1])
ax1.set_xticks(ticks)
ax1.set_xticklabels(labels, rotation=45, horizontalalignment='right')
ax1.tick_params(labelsize=8)
ax1.set_title('骑行订单时间分布',fontsize =8)
plt.vlines(32, 0, 2358, colors = "black", linestyles = "dashed",linewidth=0.8)
plt.vlines(34, 0, 1366, colors = "black", linestyles = "dashed",linewidth=0.8)
plt.vlines(72, 0, 1702, colors = "black", linestyles = "dashed",linewidth=0.8)
bbox_props=dict(boxstyle="round",fc="w",ec="0.5",alpha=0)
ax1.text(30,100,'8:00',ha='center',va='center',