\

About the author: Mort, a data analysis enthusiast, is good at data visualization, pays more attention to machine learning, and hopes to learn more and communicate with friends in the industry.

A few days ago I was browsing the official Matplotlib website and came across an interesting picture of the Logo of the Firefox browser made with Matplotlib. Is the following things (https://matplotlib.org/gallery/showcase/firefox.html#sphx-glr-gallery-showcase-firefox-py) page. At that time, I came up with an idea: I use Chrome browser most often, could I use Python to make a Logo for Chrome? So I tried it, but it was really successful.

Figure 1. Firefox logo made by Matplotlib

First, I want to show you the official Chrome logo, which is shown below. Chrome’s logo is actually very simple, much easier than Firefox’s. It’s basically red, green, yellow and the circle in the middle. The circle in the middle is easy to work with, but the red, green, and yellow parts are more complicated because each color block is an irregular shape. But if we look closely, we can see that each color block can actually be divided into two regular shapes, basically the top of a fan (i.e., the fan minus the triangle below) plus a smaller triangle, which is much easier to break down. If you don’t understand this, it doesn’t matter, we’ll analyze it step by step.

Figure 2. Chrome/Chromium official logo

The first step is to import the various packages you need.

import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle, Wedge, Polygon
Copy the code

Let’s do it step by step. So let’s start with three blocks, and as I said, each block is equal to the top of a fan plus a small triangle, so let’s draw three sectors, each of which is a third of a circle. Here’s the code.

fig, ax = plt.subplots(figsize=[8.8])
plt.ylim([0.40])
plt.xlim([0.40])
patches = []
w1 = Wedge(center=[20.20], r=10, theta1=30, theta2=150, facecolor='red',  edgecolor=None, zorder=1)
w2 = Wedge(center=[20.20], r=10, theta1=150, theta2=270, facecolor='green', edgecolor=None, zorder=1)
w3 = Wedge(center=[20.20], r=10, theta1=270, theta2=390, facecolor='yellow', edgecolor=None, zorder=1)
patches.extend([w1, w2, w3])
Copy the code

To illustrate the effect of these three color blocks, we need to add the following three lines of code. These three lines of code, we’ll call them “display code,” are used to display the results, and the reason they’re so detailed is to give you an idea of how each step works, but actually the last line of code is all connected together, and these three lines of “display code” are at the end of the whole code.

p = PatchCollection(patches, match_original=True)
ax.add_collection(p)
plt.show()
Copy the code

Figure 3. Three sectors plotted

And then I’m going to add three little triangles.

p1 = Polygon([[20- 5*3天安门事件0.5.25], [202.5*3天安门事件0.5.17.5], [20.25]],  facecolor='red', edgecolor=None, zorder=2)
p2 = Polygon([[202.5*3天安门事件0.5.17.5], [20.10], [20+2.5*3天安门事件0.5.17.5]],  facecolor='green', edgecolor=None, zorder=2)
p3 = Polygon([[20+2.5*3天安门事件0.5.17.5], [20+5*3天安门事件0.5.25], [20.25]],  facecolor='yellow', edgecolor=None, zorder=2)
patches.extend([p1, p2, p3])
Copy the code

After adding, the result is shown as the figure below.

Figure 4. 3 sectors plus 3 small triangles

It’s a little bit irregular, and maybe some of you don’t understand where these three little triangles are going. We removed the original 3 sectors and added only 3 small triangles. Then look at the result, as shown in the picture below.

Figure 5. Three small triangles drawn separately

That should make a lot of sense. The three small triangles are equilateral, with sides exactly half the length of the right side at the top of the fan. Each triangle has two sides that coincide with the right side at the top of the adjacent fan. And then we add two more circles, as follows.

c1 = Circle((20.20), 5, facecolor='white',  edgecolor=None, zorder=3)
c2 = Circle((20.20), 4, facecolor='blue',  edgecolor=None, zorder=4)
patches.extend([c1, c2])
Copy the code

The first circle is larger and white, while the second is smaller and blue. The two are concentric circles, whose centers coincide with those of the three fan-shaped circles. The completed figure is shown below. There are two things to note here. One is the zOrder attribute of each graph. The higher the number, the higher the layer, and the higher the layer, the lower the layer. Second, there is a line of code p = PatchCollection(patches, match_original=True) in the display code. Match_original means to match the attributes of the original graphics. If this attribute is not added or set to False, The original graphics Settings will not be displayed, but will be displayed in the default way. The final result may not be what we want, so be sure to set this property to True.

Figure 6. Chrome logo made by Matplotlib

Finally, add the logo of Chromium “ice version” and Canary Chrome “fried version”, just change the color of the code above, the 4 colors in ice version are “# 2976BB”, “# 75BBFD”, “# a2Cffe” and “# 1e90FF”. The four colors in the fried version are “# CB9d06,” “# FAC205,” “# F4d054” and “# F5BF03.”

Figure 7. Chromium logo made by Matplotlib

Figure 8. The Canary Chrome logo by Matplotlib

That’s it, matplotlib. Finally, there is a problem. The world’s three major browsers, Firefox and Chrome, can be made in Python, leaving Only Microsoft Edge…

Figure 9. Microsoft Edge browser logo

Appreciate the author

Python Chinese community as a decentralized global technology community, to become the world’s 200000 Python tribe as the vision, the spirit of Chinese developers currently covered each big mainstream media and collaboration platform, and ali, tencent, baidu, Microsoft, amazon and open China, CSDN industry well-known companies and established wide-ranging connection of the technical community, Have come from more than 10 countries and regions tens of thousands of registered members, members from the ministry, tsinghua university, Peking University, Beijing university of posts and telecommunications, the People’s Bank of China, the Chinese Academy of Sciences, cicc, huawei, BAT, such as Google, Microsoft, government departments, scientific research institutions, financial institutions, and well-known companies at home and abroad, nearly 200000 developers to focus on the platform.

Scan code to pay attention to the public number below reply “browser” to obtain the source code

contribute please Click to read the original article* * * *Like articles, click Looking at the