您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用Python繪制簡單的折絲圖”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
個人前面也說了強(qiáng)烈建議使用Pycharm作為Python初學(xué)者的首選IDE,主要還是因為其強(qiáng)大的插件功能,很多環(huán)境都能一鍵安裝完成,像本文的matplotlib,numpy,requests等。
下面直接上效果圖:
import matplotlib.pyplot as plt # 繪制折線圖 squares = [1, 4, 9, 16, 25] # plt.plot(squares, linewidth=5) # 指定折線粗細(xì), # #plt.show(); # # #修改標(biāo)簽文字和線條粗細(xì) # plt.title("squre number", fontsize=24) # plt.xlabel("Value", fontsize=14) # plt.ylabel("square of value", fontsize=14) # plt.tick_params(axis='both', labelsize=14) # plt.show() # 校正圖形 input_values = [1, 2, 3, 4, 5] plt.plot(input_values, squares, linewidth=5) plt.show()
生成的效果圖:
import matplotlib.pyplot as plt # 簡單的點 # plt.scatter(2, 4) # plt.show() # # # 修改標(biāo)簽文字和線條粗細(xì) plt.title("squre number", fontsize=24) plt.xlabel("Value", fontsize=14) plt.ylabel("square of value", fontsize=14) #設(shè)置刻度標(biāo)記大小 plt.tick_params(axis='both', which='major', labelsize=14) # 繪制散點 x_values = [1, 2, 3, 4, 5] y_values = [1, 4, 9, 16, 25] plt.scatter(x_values, y_values, s=100) plt.show()
import matplotlib.pyplot as plt x_values = list(range(1, 1001)) y_values = [x ** 2 for x in x_values] # y_values = [x * x for x in x_values] # y_values = [x ^ 2 for x in x_values] plt.scatter(x_values, y_values, s=40) # 坐標(biāo)軸的取值范圍 # plt.axis(0, 1100, 0, 1100000) # 依次是xmin xmax,ymin,ymax plt.show()
import matplotlib.pyplot as ply from random import choice class RandomWalk(): def __init__(self, num_points=5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk(self): # 不斷走,直到達(dá)到指定步數(shù) while len(self.x_values) < self.num_points: # 決定前進(jìn)方向以及沿這個方向前進(jìn)的距離 x_direction = choice([1, -1]) x_distance = choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x_step = x_direction * x_distance y_direction = choice([1, -1]) y_distance = choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y_step = y_direction * y_distance # 不能原地踏步 if x_step == 0 and y_step == 0: continue next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.append(next_x) self.y_values.append(next_y) rw = RandomWalk() rw.fill_walk() ply.scatter(rw.x_values, rw.y_values, s=15) ply.show()
pygal能夠繪制的圖形可以訪問pygal介紹
環(huán)境安裝,直接在Pycharm上安裝插件。
import pygal from random import randint class Die(): def __init__(self, num_sides=6): self.num_sides = num_sides; def roll(self): # 返回一個位于1和骰子面數(shù)之間的隨機(jī)值 return randint(1, self.num_sides) die = Die() results = [] # 擲100次骰子,并將結(jié)果放在列表中。 for roll_num in range(10): result = die.roll() results.append(str(result)) print(results) # 分析結(jié)果 frequencies = [] for value in range(1, die.num_sides + 1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) # 對結(jié)果進(jìn)行可視化 hist = pygal.Box() hist.title = "result of rolling one D6 1000 times" hist.x_labels = ['1', '2', '3', '4', '5', '6'] hist.x_title = "Result" hist.y_title = "frequency of result" hist.add('D6', frequencies) hist.render_to_file('die_visual.svg')
這個可以直接在Pycharm中安裝插件,非常方便。
import requests # 執(zhí)行api調(diào)用并存儲響應(yīng) url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' r = requests.get(url) print("Status code:", r.status_code) # 將api響應(yīng)存儲在一個變量中 response_dic = r.json() # 處理結(jié)果 print(response_dic.keys()) 得到結(jié)果: Status code: 200 dict_keys(['total_count', 'incomplete_results', 'items'])
# 將api響應(yīng)存儲在一個變量中 response_dic = r.json() # 處理結(jié)果 print(response_dic.keys()) print("Total repositories:", response_dic['total_count']) repo_dics = response_dic['items'] print("repositories returned:" + str(len(repo_dics))) # 研究一個倉庫 repo_dic = repo_dics[0] print("\nKeys:", str(len(repo_dic))) # for key in sorted(repo_dic.keys()): # print(key) print("Name:", repo_dic['name']) print("Owner:", repo_dic['owner']['login']) print("Starts:", repo_dic['stargazers_count']) print("Repository:", repo_dic['html_url']) print("Created:", repo_dic['created_at']) print("Updated:", repo_dic['updated_at']) print("Description:", repo_dic['description']) 得到結(jié)果: Total repositories: 2061622 repositories returned:30 Keys: 71 Name: awesome-python Owner: vinta Starts: 40294 Repository: https://github.com/vinta/awesome-python Created: 2014-06-27T21:00:06Z Updated: 2017-10-29T00:50:49Z Description: A curated list of awesome Python frameworks, libraries, software and resources
“怎么用Python繪制簡單的折絲圖”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。