preface
I recently studied the use of Pyecharts, which is a python visualization tool, and then I thought about using wechat to play together without saying anything, just to see the effect:
Environment configuration
pip install pyecharts
pip install snapshot_selenium
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install wxpy
Copy the code
Get friends
It is mainly to obtain the basic data of friends, and the data visualization code is as follows:
from wxpy import Bot, Chat
class Demo(Chat):
@staticmethod
def get_friend(a):
bot = Bot()
friends = bot.friends(update=True)
friend_data = []
for friend in friends:
if friend.sex == 1:
sex = "Male"
elif friend.sex == 2:
sex = "Female"
else:
sex = ""
friend_dict = {
"city": friend.city,
"province": friend.province,
"sex": sex,
"signature": friend.signature,
}
friend_data.append(friend_dict)
return friend_data
Copy the code
What is returned is a list of wechat friends, including their city, province, gender and personality signatures.
Geographic coordinate map
The geographic coordinate system component is used for map drawing, supporting the drawing of scatter diagrams and line sets in the geographic coordinate system.
In Pyecharts, geographic coordinates are mainly based on the Geo module
def geo_base(a):
city_data = get_data()
geo = Geo(init_opts=opts.InitOpts(theme="vintage"))
for city in city_data:
try:
geo.add_schema(maptype="china", itemstyle_opts=opts.ItemStyleOpts(color="gray"))
geo.add("Wechat Friends Distribution Map", [city], type_="effectScatter", symbol_size=10)
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Wechat Friends Distribution Map"),except Exception as e:
print(e)
pass
# geo.render("geo.html")
make_snapshot(driver, geo.render(), "geo.png")
Copy the code
This will generate a geo. PNG image in the current directory
This picture is the wechat friends distribution map of China
Heat map
Heatmap is also based on the Geo module. The only difference between type and ADD function is heatmap
The code is as follows:
def heat_map(a):
city_data = get_data()
geo = Geo(init_opts=opts.InitOpts(theme="vintage"))
for city in city_data:
try:
geo.add_schema(maptype="Guangdong", itemstyle_opts=opts.ItemStyleOpts(color="gray"))
geo.add("Heat Map of Friends in Guangdong", [city], type_="heatmap", symbol_size=10)
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Thermal map"), toolbox_opts=opts.ToolboxOpts())
except :
pass
geo.render("heat.html")
Copy the code
For example, you can select the data of a certain province, and the result after running:
Above is the heat map of wechat friends distribution in Guangdong
National distribution map
The Map is extended based on the Map module using the function add
Def add(# series name for tooltip display, legend filter. Series_name: STR, # data item (coordinate point name, coordinate point value) data_pair: Sequence, # maptype, see pyecharts.datasets.map_filenames. STR = "China ", # is_selected: bool = True, # enable mouse zooming and panning. Is_roam: bool = True, center: Optional[Sequence] = None, Zoom: Optional[Numeric] = 1, # specifies the locale name mapping name_map: Optional[dict] = None, # marks the figure shape symbol: Is_map_symbol_show: bool = True, # tag configuration item, see 'series_options.LabelOpts' label_opts: Union[opts.labelopts, dict] = opts.labelopts (), see 'series_options.tooltipopts' tooltip_opts: Union[opts.tooltipopts, dict, None] = None, # series_options.ItemStyleOpts' itemSTYLE_opts: Union[opts.ItemStyleOpts, dict, None] = None, )Copy the code
The code is as follows:
def map_base(a):
province_data = province_list()
maps = Map()
maps.add("", province_data, "china")
maps.set_global_opts(title_opts=opts.TitleOpts(title="Wechat Friend Distribution Map"), visualmap_opts=opts.VisualMapOpts())
make_snapshot(driver, geo.render(), "map.png")
Copy the code
After running, it is to generate the picture shown at the beginning of the article, isn’t it very interesting!
Word cloud
Friend city distribution word cloud
c = (
WordCloud()
.add("", city_list, word_size_range=[15.50], shape="diamond", word_gap=10)
.set_global_opts(title_opts=opts.TitleOpts(title="diamond"))
)
make_snapshot(driver, c.render(), "world.png")
Copy the code
The effect is as follows:
The bar chart
Let’s take a look at the effect:
The code is as follows:
def bar_datazoom_slider(a) -> Bar:
city_data = get_data()
c = (
Bar(init_opts=opts.InitOpts(page_title="Bar chart"))
.add_xaxis([city[0] for city in city_data])
.add_yaxis("City population", [city[1] for city in city_data])
.set_global_opts(
title_opts=opts.TitleOpts(title="Bar chart of friends' cities"),
datazoom_opts=[opts.DataZoomOpts(orient="vertical")))return c
Copy the code
Finally, another fun way to provide your wechat profile picture:
Look at the picture first:
In addition, can also customize text, will want to make their own text, input can!
Public number [Python programming and practice] background reply “image” to obtain the source code
Recommended reading
Python crawlers: An introduction to JS reverse
Create your wechat friends’ avatar wall with one click in Python
Python Data Visualization artifact – Pyecharts quick start