python画图那点事,2019美赛建模

一.3D柱状图

超级炫酷的图片,当时通宵敲代码就是为了这张图片,华而不实的图片,华丽,仅仅是为了华丽,嗯,没错,大晚上的不睡觉捣鼓这玩意儿,话不多说,上图
python画图那点事,2019美赛建模_第1张图片
是不是挺绚丽的,找个能看到全貌的角度截个图绝对是整个论文最抢眼的地方

import pandas as pd
from pyecharts import  Bar3D
data=pd.DataFrame({'Year':['2010','2011','2012','2013','2014','2015','2016','2017   ','2010','2011','2012','2013','2014','2015','2016','2017   ','2010','2011','2012','2013','2014','2015','2016','2017   ','2010','2011','2012','2013','2014','2015','2016','2017   ','2010','2011','2012','2013','2014','2015','2016','2017   '],'index':['1-VA','1-VA','1-VA','1-VA','1-VA','1-VA','1-VA','1-VA','4-OH','4-OH','4-OH','4-OH','4-OH','4-OH','4-OH','4-OH','3-WV','3-WV','3-WV','3-WV','3-WV','3-WV','3-WV','3-WV','2-KY','2-KY','2-KY','2-KY','2-KY','2-KY','2-KY','2-KY','5-PA','5-PA','5-PA','5-PA','5-PA','5-PA','5-PA','5-PA'],'P':['0.07','0.06','0.07','0.1','0.11','0.14','0.14','0.11 ','0.14','0.16','0.18','0.2','0.21','0.22','0.19','0.13 ','0.12','0.12','0.14','0.22','0.23','0.22','0.22','0.21 ','0.03','0.04','0.1','0.17','0.17','0.17','0.15','0.12   ','0.14','0.14','0.17','0.21','0.24','0.26','0.25','0.2']})
x_name=list(set(data.iloc[:,0]))
y_name=list(set(data.iloc[:,1]))
x_name.sort()
y_name.sort()
data_xyz=[]
for i in range(len(data)):
    x=x_name.index(data.iloc[i,0])
    y=y_name.index(data.iloc[i,1])
    z=data.iloc[i,2]
    data_xyz.append([x,y,z])

bar3d=Bar3D(" ",title_pos="center",width=1200,height=800)
bar3d.add(' ',x_name,y_name,data_xyz,is_label_show=True,is_visualmap=True,visual_range=[0,0.3],visual_range_color=['#FFFFFF','#00ff7f','#C0C0C0','#7093db','#8fbc8f','#871F78'],grid3d_width=150,grid3d_depth=100)
bar3d.render("JMpic1.html")

程序运行完后会生成一个JMpic.html的文件,右键copy path 在浏览器中打开

二.平面柱状图

python画图那点事,2019美赛建模_第2张图片

普通的柱状图,还是为了好看,弄个两面的,代码如下:

import matplotlib.pyplot as plt
import numpy as np

n=12    #12个柱状图
X=np.arange(n)   #生成0-11个数字
Y1=(1-X/float(n))*np.random.uniform(0.5,1.0,n)
Y2=(1-X/float(n))*np.random.uniform(0.5,1.0,n)

plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')

for x,y in zip(X,Y1):
    plt.text(x+0.4,y+0.05,'%.2f'%y,ha='center',va='bottom')

for x, y in zip(X, Y2):
    plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')

plt.xlim(5,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())

plt.show()

三.散点图

python画图那点事,2019美赛建模_第3张图片

import matplotlib.pyplot as plt
import numpy as np

#数据个数
n=2024
X=np.random.normal(0,1,n)
Y=np.random.normal(0,1,n)
T=np.arctan2(X,Y)#颜色的数量值

plt.scatter(X,Y,s=75,c=T,alpha=0.5)#大小75,颜色T,透明度50%

plt.xlim((-1,3))
plt.ylim((-1,3))
plt.xticks(())
plt.yticks(())

plt.show()

四.带坐标轴的图

python画图那点事,2019美赛建模_第4张图片

import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(-3,3,50)
y1=2*x+1
y2=x**2

#plt.figure()
#plt.plot(x,y1)

plt.figure(num=3,figsize=(8,5))##编号,大小
plt.plot(x,y2,label='up')
plt.plot(x,y1,color='red',linewidth=5.0,linestyle='--',label='down')##红色,宽度5,形状
#设置坐标轴
plt.xlim(-1,2)#取值范围
plt.ylim(-2,3)
plt.xlabel('x')#x轴
plt.ylabel('y')

new_ticks=np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-1,0,1,2,3],
           ['$a\ $','b','c','d','e'])#y轴设置成英语

#gca='get current axis'获取当前坐标轴,改变坐标轴位置
ax=plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')#上面的右边的轴消失
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
#图例
plt.legend(loc='best')


#调整清晰度
for label in ax.get_xticklabels()+ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor='white',edgecolor='None',alpha=0.7))

plt.show()

希望对大家有帮助

你可能感兴趣的:(python画图那点事,2019美赛建模)