python绘图代码⼤全_python绘图代码总结经常重复使⽤的绘图代码
使⽤SciencePlots画论⽂配图可见:传送门
折线图
import matplotlib.pyplot as plt
import matplotlib as mpl
# 中⽂和负号的正常显⽰
html网站布局制作代码Params['font.sans-serif'] = [u'SimHei']
#⾃定义刻度和标签
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(box,fc="w",ec="0.5",alpha=0)
<(30,100,'8:00',ha='center',va='center',size=8,bbox=bbox_props,horizontalalignment='left') (36,100,'8:30',ha='center',va='center',size=8,bbox=bbox_props,horizontalalignment='left') (74,100,'18:00',ha='center',va='center',size=8,bbox=bbox_props,horizontalalignment='left') ax1.legend(loc='upper right', frameon=False,fontsize = 10)
#vlines(x, ymin, ymax)画竖直线,前三个参数分别是:横坐标,minof纵坐标,max纵坐标
#hlines(y, xmin, xmax)画⽔平线
# ax1.vlines(0, 0, 0.5, colors = "c", linestyles = "dashed")
plt.savefig('./time_distribute_15min_israin_isweekday.png',format='png', dpi=300)
plt.show()
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]
ax1.set_xticks(ticks)
ax1.set_xticklabels(labels, rotation=45, horizontalalignment='right')
是⾃定义横轴的刻度显⽰间隔及显⽰标签
柱状图
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
nonsensename_list = ["< 100k","100k-200k","200k-300k","300k-400k","400k-500k","> 500k"]
num_list = [0.2626,0.3717,0.2061,0.0788,0.0343,0.0465]
def auto_text(rects,ax):
for rect in rects:
<(_x()+_width()/2, _height(), '%.2f%%'%(_height()*100), ha='center', va='bottom') def to_percent(temp,position):
return '%1.0f'%(100*temp) + '%'
with t(['science','no-latex']):
fig= plt.figure(figsize=(8, 4),dpi=200)
# 设置图形的显⽰风格
# plt.style.use('ggplot')
ax1 = fig.add_subplot(111)
rect=ax1.bar(range(len(num_list)), num_list,tick_label=name_list)
ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
ax1.set_ylabel('User Percentage',fontsize =10)
ax1.set_xlabel('Income',fontsize =10)
auto_text(rect,ax1)
# ax1.set_xticks(ticks)
# ax1.set_xticklabels(labels, rotation=45, horizontalalignment='right')
ax1.tick_params(labelsize=8)
plt.savefig('income_percentage.jpg',dpi=300,)
plt.show()
ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
作⽤是设置纵轴为百分⽐格式,可以换做,省去⾃定义函数 to_percent的⿇烦
import matplotlib.ticker as mtick
ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
多柱状图
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
# 中⽂和负号的正常显⽰
from matplotlib.ticker import FuncFormatter
def to_percent(temp,position):
return '%1.0f'%(100*temp) + '%'
ad_excel(r"C:\Users\fff507\Desktop\周内各天时变⽐例.xlsx")
value_list=data['order_id'].tolist()
x=list(range(len(value_list)))
total_width,n=1,1.5
width=total_width/n
with t(['science','no-latex']):
fig= plt.figure(figsize=(12, 4),dpi=200)
# 设置图形的显⽰风格
python代码画图案# plt.style.use('ggplot')
ax1 = fig.add_subplot(111)
ax1.bar(x,value_list,width=width,fc='red',alpha=0.5)
# 设置刻度和标签
ticks=list(range(12,len(value_list),24))
labels=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] ax1.set_xticks(ticks)
ax1.set_xticklabels(labels, rotation=0, horizontalalignment='center')
ax1.tick_params(labelsize=10)
# ax1.set_title('骑⾏订单时间分布',fontsize =10)
ax1.yaxis.set_major_formatter(FuncFormatter(to_percent))
byte是什么数据类型x1=0-width/2
plt.vlines(x1, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(23-width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(47+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(71+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(95+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(119+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
mfc界面开发plt.vlines(143+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
plt.vlines(167+width/2, 0, 0.2, colors = "grey", linestyles = "-",linewidth=1)
list2=list(range(12,157,24))
for i in list2:
plt.vlines(i, 0, 0.2, colors = "grey", linestyles = "--",linewidth=1)
ax1.set_ylabel('Trip Percentage/Day',fontsize =10)
# plt.savefig('周内各天时变⽐例.png',format='png',bbox_inches='tight', pad_inches = 0,dpi=300) plt.savefig('周内各天时变⽐例.png',format='png',dpi=300)
plt.show()
上⾯⽤到了SciencePlots绘图,传送门,会⾃动去掉绘图时的⽩边,不⽤这个的话,下⾯这样也可以去掉⽩边plt.savefig('周内各天时变⽐例.png',format='png',bbox_inches='tight', pad_inches = 0,dpi=30
0)
概率分布直⽅图&累计概率分布
def cum_prob_curve(data,bins,title,xlabel,pic_path):
'''
绘制概率分布直⽅图和累计概率分布曲线
'''
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.ticker import FuncFormatter
#从pyplot导⼊MultipleLocator类,这个类⽤于设置刻度间隔
from matplotlib.pyplot import MultipleLocator
哪种编程语言最好学fig= plt.figure(figsize=(8, 4),dpi=100)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。