绘图可视化GitHub存储库

题目:

(英文版)    ”Write a program to visualize the Github Repositories by using Plotly. Instead of drawing the histogram with the stars of repositories, please draw with the data about "forks".“

(中文版,翻译英文版而得的) “编写一个程序,通过使用绘图来可视化 Github 存储库。请不要用存储库的星星绘制直方图,请使用有关"叉子"的数据进行绘制。”   

代码如下:

import requests
import plotly.graph_objs as go

# Set up the GitHub API endpoint and parameters
url = "https://api.github.com/search/repositories"
params = {"q": "language:python", "sort": "forks"}

# Send a GET request to the API
response = requests.get(url, params=params)

# Extract the JSON data from the response
data = response.json()

# Get the list of repositories from the JSON data
repos = data["items"]

# Extract the repository names and fork counts
names = [repo["name"] for repo in repos]
forks = [repo["forks_count"] for repo in repos]

# Create a bar chart of the repository forks using Plotly
trace = go.Bar(x=names, y=forks)
layout = go.Layout(title="Python Repositories by Forks", xaxis=dict(title="Repository"), yaxis=dict(title="Forks"))
fig = go.Figure(data=[trace], layout=layout)
fig.show()

在这个程序中,我们首先为GitHub API设置端点和参数,并发送GET请求以检索按分叉数排序的Python存储库的数据。然后,我们从JSON数据中提取存储库名称和分支计数,并使用Plotly创建分支的条形图。
生成的可视化结果应显示在新的浏览器窗口中。您可以通过将鼠标悬停在条形图上查看存储库名称和分叉计数来与图表交互。您还可以放大和缩小,并将图表下载为PNG图像。