In [ ]:
import matplotlib
matplotlib.use('TkAgg')
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import datawash
In [ ]:
# 遍历所有用户,读取需要的信息
list1 = list()
list2 = list()
list3 = list()
jsons = datawash.datajsons()
for user in jsons:
try:
list1.append([user['voteupCount'],user['thankedCount']])
list2.append([user['followingCount'],user['followerCount']])
list3.append([user['favoriteCount'],user['favoritedCount']])
except:
pass
In [ ]:
# 绘图用到的颜色
black = '#212121'
gray = '#727272'
red = '#D32F2F'
orange = '#FF9500'
orange2 = '#FFF1DE'
green = '#99FF33'
brown = '#cc6600'
In [ ]:
# 100000以下赞同数和感谢数空间分布
fig = plt.figure()
ax = Axes3D(fig)
# 生成网格上所有点的坐标
start = 0 # 起始点
stop = 100000 # 终止点
step = int(stop/70) # 步长
X = np.arange(start, stop, step)
Y = np.arange(start, stop, step)
X, Y = np.meshgrid(X, Y)
Z = np.zeros_like(X)
for i in list1:
if i[0] >= start and i[0] < stop and i[1] >= start and i[1] < stop:
Z[int((i[1]-start)/step),int((i[0]-start)/step)] += 1
# 取对数,使图案更平缓
Z = np.log10(Z+1)
# 绘图
ls = matplotlib.colors.LightSource(270, -60)
rgb = ls.shade(Z, cmap=plt.cm.gist_earth_r,vert_exag=0.1, blend_mode='soft')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=rgb,linewidth=0, antialiased=True, shade=True)
# 设置Z轴刻度
zticks = np.arange(0, Z.max(), int(Z.max()/5))
zlabels = [int(10**x) for x in zticks]
ax.set_zticks(zticks)
ax.set_zticklabels(zlabels)
# 设置标题,标签
ax.set_title('100000以下赞同数和感谢数空间分布', color=orange, fontsize=20)
ax.set_xlabel('赞同数', fontsize=14)
ax.set_ylabel('感谢数', fontsize=14)
ax.set_zlabel('用户数量(人)', fontsize=14)
ax.set_facecolor(orange2)
ax.invert_yaxis()
fig.set_size_inches(12,8)
fig.show()
In [ ]:
# 10000以下关注数和关注者数空间分布
fig = plt.figure()
ax = Axes3D(fig)
# 生成网格上所有点的坐标
start = 0 # 起始点
stop = 10000 # 终止点
step = int(stop/70) # 步长
X = np.arange(start, stop, step)
Y = np.arange(start, stop, step)
X, Y = np.meshgrid(X, Y)
Z = np.zeros_like(X)
for i in list2:
if i[0] >= start and i[0] < stop and i[1] >= start and i[1] < stop:
Z[int((i[1]-start)/step),int((i[0]-start)/step)] += 1
# 取对数,使图案更平缓
Z = np.log10(Z+1)
# 绘图
ls = matplotlib.colors.LightSource(270, -60)
rgb = ls.shade(Z, cmap=plt.cm.gist_earth_r,vert_exag=0.1, blend_mode='soft')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=rgb,linewidth=0, antialiased=True, shade=True)
# 设置Z轴刻度
zticks = np.arange(0, Z.max(), int(Z.max()/5))
zlabels = [int(10**x) for x in zticks]
ax.set_zticks(zticks)
ax.set_zticklabels(zlabels)
# 设置标题,标签
ax.set_title('10000以下关注数和关注者数空间分布', color=orange, fontsize=20)
ax.set_xlabel('关注数', fontsize=14)
ax.set_ylabel('关注者数', fontsize=14)
ax.set_zlabel('用户数量(人)', fontsize=14)
ax.set_facecolor(orange2)
ax.invert_yaxis()
fig.set_size_inches(12,8)
fig.show()
In [ ]:
# 10000以下收藏答案数和答案被收藏数空间分布
fig = plt.figure()
ax = Axes3D(fig)
# 生成网格上所有点的坐标
start = 0 # 起始点
stop = 10000 # 终止点
step = int(stop/70) # 步长
X = np.arange(start, stop, step)
Y = np.arange(start, stop, step)
X, Y = np.meshgrid(X, Y)
Z = np.zeros_like(X)
for i in list3:
if i[0] >= start and i[0] < stop and i[1] >= start and i[1] < stop:
Z[int((i[1]-start)/step),int((i[0]-start)/step)] += 1
# 取对数,使图案更平缓
Z = np.log10(Z+1)
# 绘图
ls = matplotlib.colors.LightSource(270, -60)
rgb = ls.shade(Z, cmap=plt.cm.gist_earth_r,vert_exag=0.1, blend_mode='soft')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=rgb,linewidth=0, antialiased=True, shade=True)
# 设置Z轴刻度
zticks = np.arange(0, Z.max(), int(Z.max()/5))
zlabels = [int(10**x) for x in zticks]
ax.set_zticks(zticks)
ax.set_zticklabels(zlabels)
# 设置标题,标签
ax.set_title('10000以下收藏答案数和答案被收藏数空间分布', color=orange, fontsize=20)
ax.set_xlabel('收藏答案数', fontsize=14)
ax.set_ylabel('答案被收藏数', fontsize=14)
ax.set_zlabel('用户数量(人)', fontsize=14)
ax.set_facecolor(orange2)
ax.invert_yaxis()
fig.set_size_inches(12,8)
fig.show()
In [ ]:
In [ ]: