This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
To get the data
Data: Title name, author, grade, how many people have seen STATS
Get the 10 authors with the highest ratings
GradeDict gets the average score for each author, and uses the sorted() function to get the top 10 values in gradeDict
df = pd.read_csv("clean_data.csv")
gradeDict = {}
countDict = {}
for author, grade in zip(df.author, df.grade):
if author not in countDict.keys():
countDict[author] = 1
gradeDict[author] = grade
else:
countDict[author] = countDict[author] + 1
gradeDict[author] = gradeDict[author] + grade
for key in gradeDict.keys():
gradeDict[key] = gradeDict[key] / countDict[key]
gradeList = sorted(gradeDict.items(), key=lambda x: x[1], reverse=True)[0:10]
countList = sorted(countDict.items(), key=lambda x: x[1], reverse=True)[0:10]
labels = []
values = []
for i in countList:
labels.append(i[0])
values.append(i[1])
Copy the code
Labels and values
The pie chart
Explode indicates the interval between sectors. The default value is 0
Explode = [0.05] for I in range(1, 10): explode. Append (0.02)Copy the code
Change the Chinese font style, here set to SimHei font, can also be changed to other, otherwise it will be garbled
plt.rcParams['font.sans-serif'] = 'SimHei'
Copy the code
Draw the pie chart, autopct sets the pie chart in each sector percentage display format, % 0.2F %% two decimal percentage. Pctdistance specifies the autopct position scale, the default value is 0.6, the percentage distance from the center of the circle, the smaller the value is, the closer it is to the center of the circle, the drawing position marked by labelDistance, the ratio to the radius, the default value is 1.1, if <1, it is drawn in the inside of the pie chart. That’s where the author is
Plt.savefig saves the image as an image, but be sure to place it before plt.show(), otherwise the save will be blank
plt.pie(values, labels=labels, explode=explode, autopct="%.2f%%", Pctdistance =0.7) plt.title(" top 10 published articles ") plt.savefig(" pie.png ") plt.show()Copy the code
Bar charts
Plt. bar draws horizontal bar chart, plt.barh draws vertical bar chart, can change the color of the bar chart or the color of each bar chart by color; The bar() method uses width and the barh() method uses height
# plt.barh(range(len(labels)), values, tick_label=labels)
# plt.bar(range(len(labels)), values, tick_label=labels, color="#4CAF50")
plt.bar(range(len(labels)), values, tick_label=labels)
Copy the code
Plt. xticks set the label on the X axis, which is available by default. Here, change the Angle of the label to 45 degrees. Plt. yticks changed the scale of the label on the Y axis to make it more beautiful. Plt. text displays the specific numbers on the bar chart
plt.xticks(range(len(labels)), labels, rotation=45) plt.yticks([0, 2, 4, 6, 8, 10]) for x, y in zip(range(len(labels)), values): Plt.text (x, y+0.1, "%d" % y, ha="center") plt.title(" top 10 posts ") plt.xlabel(" author ") plt.ylabel(" posts ") plt.show()Copy the code
The line chart
Plt.legend () =u’111′; For example, ‘go-‘ is a solid line of green dots
# simulated data labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] values222 = [5, 7, 9, 7, 5, 3, 4, 8, 6, 3] PLT. The plot (labels, values, 'go-', label=u'111') plt.plot(labels, values222, 'b*-', label=u'222') plt.xticks(labels) for x, Y in zip(range(len(labels)), values): plt.text(x, y+0.1, "%.2f" % y, ha="center") plt.legend() #Copy the code
🎈🎈🎈 has slipped away