用matplotlib进行数据可视化并储存图片,附提高图片分辨率方法

20201118初稿
昂,这算是个摘要好了,具体内容得往下看。
每一次画图前,记得调用plt.cla()清除一下画布。。。不然会很难看的。。。
让保存的图片更清晰,可以在保存的时候修改dpi参数还有另外一个bbox_inches="tight"这样子
过段时间贴代码好了!

20201126贴代码

背景

这其实是两个实验。第一个实验是要基于我前好几个博客提到的那个制作停留点用于可视化的输出的数据文件来画出停留时长在10/20/30/。。。等时间段的停留车数目。第二个是要画一下停留的图。

在这之前,我把stop.txt转换成了json文件,不细讲了,用c++嗯转json,就嗯转……

代码

尝试了一下英文做注释,毕竟pycharm是英文的,感觉比较协调,也不容易出现换个编辑器就乱码的情况比如用matplotlib进行数据可视化并储存图片,附提高图片分辨率方法_第1张图片
呜呜呜,就试试用英文注释吧。

这个里面,用到了python里的json库,math,numpy,matplotlib库。用json库里的方法导入json、处理json、导出json都是很方便的,而且有许多前辈已经讲过了使用方法,在这里就不细说了。

#lab1

# The unit of stopTime is seconds.
import json
import math
import numpy as np
from matplotlib import pyplot as plt

#input json file
data = json.load(open("stopJson.json"))

#stopTime storage
array_stopTime=[]

#calculate all stopTime
#I think I can do a lot in this loop
#but I can't because subsequent operations are done by results from previous steps
for i in data["points"]:
    array_stopTime.append(i["stopTime"])

#calculate final stopTime array
array_stopTimeInNumpy = np.array(array_stopTime)

#calculate max index of stopTime
maxIndexOfStopTime = np.argmax(array_stopTimeInNumpy)

#final draw-used array which stored the quantity of cars in a specific range of stopTime
stopMinutesNumCars = []
endTime= math.floor(data["points"][np.argmax(array_stopTimeInNumpy)]["stopTime"] / 60) + 1

for i in range(0,endTime):
    stopMinutesNumCars.append(0)

#calculate those counts of stopTime
for i in array_stopTimeInNumpy:
    stopMinutesNumCars[math.floor(i / 60)]+=1

#result test - success
#print(stopMinutesNumCars[0])
#print(stopMinutesNumCars[1])

#generate x and y of target bar chart
x = []
y = []
for i in range(0,endTime-10,10):
    x.append(i)
    y.append(stopMinutesNumCars[i]
             +stopMinutesNumCars[i+1]
             +stopMinutesNumCars[i+2]
             +stopMinutesNumCars[i+3]
             +stopMinutesNumCars[i+4]
             +stopMinutesNumCars[i+5]
             +stopMinutesNumCars[i+6]
             +stopMinutesNumCars[i+7]
             +stopMinutesNumCars[i+8]
             +stopMinutesNumCars[i+9])

#check up x and y
#by checking x and y I found that the quantity of cars which stop time is between 0 and 10 minutes
#are far more than others
#print(x)
#print(y)

#draw bar chart
plt.bar(x,y)
plt.xlabel("停留时间长度")
plt.ylabel("平均停留点数量")
plt.show()

#generate json file
#I changed the json file manually by adding a key called "points".
#I can use CSS to draw the bar chart as well.
dataDrawUse=[]
for i in range(0,len(x)):
    dataDrawUse.append([x[i],y[i]])

data1 = json.dumps(dataDrawUse)
with open("package.json","w",encoding="utf-8") as f:
    f.write(json.dumps(data1,indent=4))
# lab2
import json
import numpy as np
from matplotlib import pyplot as plt
import math
from datetime import datetime
dt=datetime.now()

# load data file
data=json.load(open("sortedStopTime.json"))

# left top       120.0318 31.7551
# right bottom   122.1779 31.3177
# Not those. The abscissa is arranged in descending order but the ordinate isn't.
# so I used numpy to find out the left top and the right bottom.
# coordinate storage
abscissa = []
ordinate = []
# load coordinates
for i in data:
    abscissa.append(i["coordinate"][0])
    ordinate.append(i["coordinate"][1])
# calculate coordinates
abscissa_np=np.array(abscissa)
ordinate_np=np.array(ordinate)
leftTopX=abscissa_np[np.argmin(abscissa_np)]
leftTopY=ordinate_np[np.argmin(ordinate_np)]
rightBottomX=abscissa_np[np.argmax(abscissa_np)]
rightBottomY=ordinate_np[np.argmax(ordinate_np)]

#drawing

# calculate end time  unit:minute
endTime=math.floor(data[-1]["stopTime"]/60)+1

# math.floor(data["points"][np.argmax(array_stopTimeInNumpy)]["stopTime"] / 60) + 1
local_x=[]
local_y=[]
local_i=0

for j in range(10,endTime-10,10):
    for i in range(local_i,len(data)):
        if (data[i]["stopTime"]/60)<j:
            local_x.append(data[i]["coordinate"][0])
            local_y.append(data[i]["coordinate"][1])

        else:
            local_i = i
            break

    plt.cla()
    plt.xlim(xmax=122.3,xmin=119.80)
    plt.ylim(ymax=32.20,ymin=29.93)
    plt.scatter(local_x, local_y, s=0.3, marker="o", color="blue")
    #plt.show()
    
    
#这里还是要讲一下这个储存图片的,这里图片的默认分辨率是640x480,极为不清晰。。。
#所以可以采用如下方法。在plt.savefig()中加上后两个参数:dpi=700,bbox_inches="tight"就可以了。
#dpi越高貌似是越清晰的,但是图片占用储存空间也越大。
#dpi不是鼠标那个dpi,许多前辈也讲过dpi与像素和实际尺寸的转换方法,这里也就不细说了。
#如何在plt.show()中展示出清晰一些的图,还是没太弄明白。
    plt.savefig(r"dotPics/"+str(j)+".png",dpi=700,bbox_inches="tight")
    local_x.clear()
    local_y.clear()
    if j/10%10==0:
        print("now is: "+str(j)+"photos")
        print(dt.strftime("%Y-%m-%d %H:%M:%S"))

——来自一个学不会大物的小笨鸟。

你可能感兴趣的:(python,数据可视化,pycharm)