一些调试通过的绘图python代码

%matplotlib inline
%config InlineBackend.figure_format = "retina"

 

%matplotlib inline
import pandas as pd
from ggplot import *
meat_lng = pd.melt(meat[['date','beef','pork','broilers']],id_vars='date')
ggplot(aes(x='date',y='value',colour='variable'),data = meat_lng)+geom_point(color='red')

 

import matplotlib.pyplot as plt
import numpy as np
##设置字体
from matplotlib.font_manager import FontProperties
font = FontProperties(fname = "C:/Windows/Fonts/方正粗黑宋简体.ttf",size= 14)
%matplotlib inline
%config InlineBackend.figure_format = "retina"
t = np.arange(1,10,0.05)
x= np.sin(t)
y = np.cos(t)
plt.figure(figsize=(8,5))
##绘制一条线
plt.plot(x,y,"r-*")
plt.axis("equal")
plt.xlabel("正弦",fontproperties=font)

plt.xlabel("余弦",fontproperties=font)
plt.title("一个圆形",fontproperties=font)
plt.show()

 

##subplot
import numpy as np 
import matplotlib.pyplot as plt
##生成x
x1 = np.linspace(0.0,5.0)
x2 = np.linspace(0.0,2.0)
## 生成y
y1 = np.cos(2*np.pi*x1)*np.exp(-x1)
y2 = np.cos(2*np.pi*x2)
##绘制第一个图
plt.subplot(2,1,1)
plt.plot(x1,y1,'yo-')
plt.title('A tale of 2 subolots')
plt.ylabel('Damped oscillation')
##绘制第二个子图
plt.subplot(2,1,2)
plt.plot(x2,y2,'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()

 

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
#example data
mu = 100
sigma = 15
x = mu +sigma*np.random.randn(10000)
print("x:",x.shape)
num_bins = 50
n,bins,patches = plt.hist(x,num_bins,normed = 1,facecolor = 'green',alpha=0.5)
#添加一个最佳拟合曲线
y = mlab.normpdf(bins,mu,sigma)
##返回关于数据的pdf数值(概率密度函数)
plt.plot(bins,y,'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ:$\mu=100$,$\sigma=15$')
plt.subplots_adjust(left=0.15)
plt.show()
print("bind:\n",bins)

 

import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
##生成数据
delta = 0.2
x = np.arange(-3,3,delta)
y = np.arange(-3.3,delta)
X,Y = np.meshgrid(x,y)
Z = X**2+Y**2
x = X.flatten()
#返回一维的数组,但该函数只能适用于numpy对象(array或者mat)
y=Y.flatten()
z=Z.flatten()
fig = plt.figure(figsize = (12,6))
ax1 = fig.add_subplot(121,projection='3d')
ax1.plot_trisurf(x,y,z,cmap=cm.jet,linewidth=0.01)
#cmap指颜色,默认绘制为RGB(A)颜色空间,jet表示“蓝-青-黄-红”颜色
plt.title("3D")
ax2=fig.add_subplot(122)
cs=ax2.contour(X,Y,Z,15,cmap='jet',)
ax2.clabel(cs,inline=True,fontsize=10,fmt='%1.1f')
plt.title("Contour")
plt.show()

 

 

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure(figsize = (8,6))
ax = fig.gca(projection='3d')
X,Y,Z = axes3d.get_test_data(0.05)
ax.plot_surface(X,Y,Z,rstride= 8,cstride = 8,alpha=0.3)
cset=ax.contour(X,Y,Z,zdir = 'z',offset=-100,cmap=cm.coolwarm)
cset=ax.contour(X,Y,Z,zdir = 'x',offset=-40,cmap=cm.coolwarm)
cset=ax.contour(X,Y,Z,zdir = 'y',offset=40,cmap=cm.coolwarm)
ax.set_xlabel('X')
ax.set_xlim(-40,40)
ax.set_ylabel('Y')
ax.set_ylim(-40,40)
ax.set_zlabel('Z')
ax.set_ylim(-100,100)
plt.show()

 

import numpy as np
import matplotlib.pyplot as plt
n_groups =5
means_men=(20,35,30,35,27)
std_men=(2,3,4,1,2)
means_women=(25,32,34,20,25)
std_women = (3,5,2,3,3)
fig,ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor':'0.3'}
rects1 = plt.bar(index,means_men,bar_width,alpha=opacity,color='b',yerr=std_men,error_kw=error_config,label='Men')
rects2 = plt.bar(index+bar_width,means_women,bar_width,alpha=opacity,color='r',yerr=std_women,error_kw=error_config,label='Women')
plt.xlabel('Group')
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(index+bar_width,('A','B','C','D','E'))
plt.legend()
plt.tight_layout()
plt.show()

 

import numpy as np
import matplotlib.pyplot as plt

labels = 'Frogs','Hogs','Dogs','Logs'
sizes = [15,30,45,10]
colors = ['yellowgreen','gold','lightskyblue','lightcoral']
explode = (0,0.1,0,0)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct = '%1.1f%%',shadow = True,startangle = 90)
plt.axis('equal')
plt.show()

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df_data = pd.read_csv('https://raw.githubusercontent.com/pydata/pandas/master/pandas/tests/data/iris.csv')
df_data.head()
fig,ax = plt.subplots()
colors =15* ["yellowgreen","gold","green","blue","red","black",
          'lightskyblue','lightcoral','yellow','pink']

ax.scatter(df_data['SepalLength'],df_data['SepalWidth'],s= df_data['PetalLength']*100,color = colors,alpha = 0.6)
ax.set_xlabel('SepalLength(cm)')
ax.set_ylabel('SepalWidth(cm)')
ax.set_title('PetalLength(cm)*100')

ax.grid(True)
fig.tight_layout()
plt.show()

 

from sklearn.datasets import load_iris
import numpy as np
iris = load_iris()
iris.data
iris
from pandas import DataFrame
df = DataFrame(iris.data,columns = iris.feature_names)
df['target'] = iris.target
df
import numpy as np
import pandas as pd
from scipy import stats,integrate
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = "retina"
import seaborn as sns
sns.set(color_codes = True)
sns.distplot(df["petal length (cm)"],bins = 15)
plt.show()

import networkx as nx
import matplotlib.pyplot as plt
G = nx.random_geometric_graph(200,0.125)
pos = nx.get_node_attributes(G,'pos')
dmin = 1
ncenter = 0
for n in pos:
    x,y = pos[n]
    d = (x-0.5)**2+(y-0.5)**2
    if d<dmin:
        ncenter = n
        dmin = d
p = nx.single_source_shortest_path_length(G,ncenter)
plt.figure(figsize = (8,8))
nx.draw_networkx_edges(G,pos,nodelist = [ncenter],alpha = 0.4)
nx.draw_networkx_nodes(G,pos,nodelist = p.keys(),node_size = 80)
plt.xlim(-0.05,1.05)
plt.ylim(-0.05,1.05)
plt.axis('off')
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值