当前位置:网站首页>Matplotlib data visualization three minutes entry, half an hour enchanted?
Matplotlib data visualization three minutes entry, half an hour enchanted?
2022-07-25 14:06:00 【Ride Hago to travel】
One , First of all, of course, the configuration environment
stay Windows Of DOM Enter the following command in the window ( stay pycharm Below Terminal Chung Tung )
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
( notes : If Xiaobian remembers correctly , This bag has more than fifty M, If you do not add domestic image source , It will continue to download for several hours ...)
Two ,3 Minute quick start
1, The import module
import matplotlib.pyplot as plt # Import matplotlib modular
# Import numpy and pandas modular , You may use
import numpy as np
import pandas as pd
2, Several common function images (y=x, y=x^2 +3, y=sin(x))
① y=x Images
x = np.linspace(-10, 10, 10) # Set up y=x Of x Axis
# plt.plot() It means drawing a line chart , first x Express x Value on axis , the second x Express y Value on axis ( because y=x Of x Axis and y It's like a shaft ),
# marker Represents the mark shape corresponding to each selected point , color='r' Indicates that the line color is red Red , linestyle='--' Represents a broken line
# The line style of is '--', linewidth Indicates the line width of the polyline , label The label text that represents the content of the marked graphic
plt.plot(x, x, marker='o', color='r', linestyle='--', linewidth=1, label='y=x')
plt.legend()
plt.show() # Show pictures

notes : Code shorthand
plt.plot(x, x, marker='o', color='r', linestyle='--', linewidth=1, label='y=x')
plt.plot(x, x, marker='o', c='r', ls='--', lw=1, label='y=x') # The effect same as above
plt.plot(x, x, 'o--r', lw=1, label='y=x') # ditto , namely marker,color,linestyle The parameters in can be mixed together , Don't distinguish the order ( But you can't put -- Two of them - Write separately )
notes : About marker,color, linestyle There are many more parameters in , The contents in the help manual are as follows
**Markers**
============= ===============================
character description
============= ===============================
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'8'`` octagon marker
``'s'`` square marker
``'p'`` pentagon marker
``'P'`` plus (filled) marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'X'`` x (filled) marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
**Line Styles**
============= ===============================
character description
============= ===============================
``'-'`` solid line style
``'--'`` dashed line style
``'-.'`` dash-dot line style
``':'`` dotted line style
============= ===============================
**Colors**
The supported color abbreviations are the single letter codes
============= ===============================
character color
============= ===============================
``'b'`` blue
``'g'`` green
``'r'`` red
``'c'`` cyan
``'m'`` magenta
``'y'`` yellow
``'k'`` black
``'w'`` white
============= ===============================
② y=x^2 +3 Images
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-10, 10, 100) # Set up x Shaft value
y = x**2 + 3 # Set up y Shaft value
plt.plot(x, y, '--g', lw=1, label='y=x^2+3')
# size Indicates the title of the graphic content
plt.title(' Broken line diagram ', c='r', size=15)
plt.legend()
plt.show() # Show pictures

③ y=sin(x) Images
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-10, 10, 100)
y = np.sin(x)
plt.plot(x, y, '--g', lw=1, label='y=sin(x)')
plt.title(' Broken line diagram ', c='r', size=15)
plt.legend()
plt.xlabel(' This is a x Axis ', c='b', size=13) # Set up x Label text for axis
plt.ylabel(' This is a y Axis ', c='k', size=13) # Set up y Label text for axis
plt.xlim(-2*np.pi, 2*np.pi) # Set up x Axis display range
plt.ylim(-1, 1) # Set up y Axis display range
plt.xticks(np.arange(-2*np.pi, 2*np.pi, np.pi/4), rotation=45) # Limit x Display density of axis ,rotation=45 Express x The scale font on the axis rotates 45°
plt.yticks(np.arange(-1, 1.1, 0.2)) # Limit y Display density of axis
plt.show()

④ take 3 A picture composes a picture
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-5, 5, 100)
y1 = x
y2 = x**2 + 3
y3 = np.sin(x)
plt.figure(figsize=(9, 6)) # Set canvas size ,9 namely 900px,6 namely 600px( Don't set it too big )
plt.plot(x, y1, ':b', lw=2, label='y=x')
plt.plot(x, y2, '-r', lw=1, label='y=x^2+3')
plt.plot(x, y3, '--g', lw=1.5, label='y=sin(x)')
plt.title(' Broken line diagram ', c='r', size=15)
plt.legend()
plt.xlabel(' This is a x Axis ', c='b', size=13)
plt.ylabel(' This is a y Axis ', c='k', size=13)
plt.show()

⑤ Draw multiple pictures on one canvas
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-5, 5, 100)
y1 = x
y2 = x**2 + 3
y3 = np.sin(x)
y4 = np.cos(x)
pf = plt.figure(figsize=(10, 7))
as1 = pf.add_subplot(2, 2, 1) # Indicates that the drawing is divided into 2 That's ok 2 Column ,as1 In the 1 A place
as2 = pf.add_subplot(2, 2, 2) # Indicates that the drawing is divided into 2 That's ok 2 Column ,as2 In the 2 A place
as3 = pf.add_subplot(2, 2, 3) # Indicates that the drawing is divided into 2 That's ok 2 Column ,as3 In the 3 A place
as4 = pf.add_subplot(2, 2, 4) # Indicates that the drawing is divided into 2 That's ok 2 Column ,as4 In the 4 A place
as1.plot(x, y1, ':b', lw=2, label='y=x')
as2.plot(x, y2, '-r', lw=1, label='y=x^2+3')
as3.plot(x, y3, '--g', lw=1.5, label='y=sin(x)')
as4.plot(x, y4, '-.k', lw=1.5, label='y=cos(x)')
as1.legend()
as2.legend()
as3.legend()
as4.legend()
plt.show()

3、 ... and , Draw a histogram
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(1, 13)
y = np.random.randint(1, 50, 12)
lab = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
plt.figure(figsize=(9, 6.5))
plt.bar(x, y, color='b', tick_label=lab, label=' Bar charts ')
plt.xticks(np.arange(1, 13))
plt.title(' Column chart exercise ', color='b', size=13)
plt.legend()
plt.xlabel(' Case number ')
plt.ylabel(' Box size ')
plt.show()

Four , Draw a pie chart
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
kinds = [' Siberian Husky ', ' Alaska ', ' Samoye ', ' Yellow lion king ']
num = [0.3, 0.1, 0.2, 0.4]
plt.figure(figsize=(9, 6.5)) # plt.figure It means drawing a pie chart
# explode=[0, 0, 0.1, 0] Highlight
plt.pie(num, labels=kinds, autopct='%.2f%%', explode=[0, 0, 0.1, 0])
plt.show()

5、 ... and , A profound
1, Crawl data
Climb the price of vegetables in a wholesale market for nearly a year
import requests
from lxml import etree
import csv
from concurrent.futures import ThreadPoolExecutor
import time
f = open("vegetables.csv", mode="w", newline='', encoding="utf-8")
csv_write = csv.writer(f)
def down_one_page(url):
res = requests.get(url=url, headers=headers).text
# print(res)
html = etree.HTML(res)
table = html.xpath('/html/body/div[2]/div[4]/div[1]/table')[0]
# print(table)
# trs = table.xpath('./tr')[1:]
trs = table.xpath('./tr[position()>1]')
# print(len(trs))
for tr in trs:
txt = tr.xpath('./td/text()')
# Get rid of \\ /
txt = (item.replace("\\", "").replace("/", "") for item in txt)
# print(list(txt))
csv_write.writerow(list(txt))
# print(list(txt))
new_name = url.split("/")[-1].split(".")[0]
print(" The first \033[31;1m%s\033[0m Page download complete !!!" % new_name)
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54"
}
# for i in range(1, 14870):
# down_one_page("http://www.xinfadi.com.cn/marketanalysis/0/list/%s.shtml" % i)
user_input = int(input("Total 15069 pages,please input a page number>>>:"))
start_time = time.time()
with ThreadPoolExecutor(32) as t:
for i in range(1, user_input + 1):
t.submit(down_one_page, "http://www.xinfadi.com.cn/marketanalysis/0/list/%s.shtml" % i)
print("Download over,it takes \033[31;1m%s\033[0m seconds" % (time.time() - start_time))

about 10 Ten thousand data !
2, Simple processing of data
First read the first few lines to see the data
import pandas as pd
output_file = 'vage.csv'
vege_data = pd.read_csv('vegetables.csv', header=None)
print(vege_data.head(5))
0 1 2 3 4 5 6
0 Samito 20.0 22.5 25.0 Cherries Jin 2021-05-15
1 Sheep's brain 42.0 42.5 43.0 Mutton Jin 2021-05-15
2 Sheep bone 11.0 11.5 12.0 Mutton Jin 2021-05-15
3 Lamb belly 32.0 32.0 32.0 Mutton Jin 2021-05-15
4 Lamb fillet 42.0 42.0 42.0 Mutton Jin 2021-05-15
For the convenience of drawing in the back , Sort the time column
import pandas as pd
output_file = 'vage.csv'
vege_data = pd.read_csv('vegetables.csv', header=None, index_col=6).sort_index()
print(vege_data.head(5))
0 1 2 3 4 5
6
2020-05-13 Thread eggplant 1.0 1.50 2.0 | Yue Yun Jin
2020-05-13 Chrysanthemum 1.2 1.60 2.0 liao Jin
2020-05-13 Lettuce 1.2 1.50 1.8 liao Jin
2020-05-13 Water chestnut 1.8 1.90 2.0 Wan Jin
2020-05-13 Golden pumpkin 0.9 1.05 1.2 Joan cantaloupe Jin
select ‘ Whole cattle ’ All the corresponding lines
import pandas as pd
output_file = 'vage.csv'
vege_data = pd.read_csv('vegetables.csv', header=None, index_col=6).sort_index()
new_data = vege_data[vege_data[0] == ' Whole cattle ']
print(new_data)
0 1 2 3 4 5
6
2020-05-15 Whole cattle 30.00 30.50 31.00 Ordinary Jin
2020-05-16 Whole cattle 30.00 30.50 31.00 Ordinary Jin
2020-05-17 Whole cattle 30.00 30.50 31.00 Ordinary Jin
2020-05-18 Whole cattle 30.50 30.75 31.00 Ordinary Jin
2020-05-19 Whole cattle 30.50 30.75 31.00 Ordinary Jin
... .. ... ... ... ... ..
2021-05-12 Whole cattle 34.00 34.00 34.00 Beef Jin
2021-05-13 Whole cattle 34.00 34.00 34.00 Beef Jin
2021-05-14 Whole cattle 34.25 34.25 34.25 Beef Jin
2021-05-15 Whole cattle 34.25 34.25 34.25 Beef Jin
2021-05-16 Whole cattle 34.25 34.25 34.25 Beef Jin
[250 rows x 6 columns]
It is found that there is a year's data , only 250 That's ok , Save it before you look
import pandas as pd
output_file = 'vage.csv'
vege_data = pd.read_csv('vegetables.csv', header=None, index_col=6).sort_index()
# print(vege_data.head(5))
new_data = vege_data[vege_data[0] == ' Whole cattle ']
# print(new_data)
new_data.to_csv(output_file, header=False, encoding='utf-8')

During observation, I found that there was no price for a period of time 
So take 2020-09-30 Data after this time period
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('vage.csv', header=None)[29:]
print(data)
0 1 2 3 4 5 6
29 2020-09-30 Whole cattle 34.00 34.00 34.00 Ordinary Jin
30 2020-10-01 Whole cattle 34.00 34.00 34.00 Ordinary Jin
31 2020-10-02 Whole cattle 33.00 33.50 34.00 Ordinary Jin
32 2020-10-03 Whole cattle 33.00 33.50 34.00 Ordinary Jin
33 2020-10-04 Whole cattle 33.00 33.50 34.00 Ordinary Jin
.. ... .. ... ... ... ... ..
245 2021-05-12 Whole cattle 34.00 34.00 34.00 Beef Jin
246 2021-05-13 Whole cattle 34.00 34.00 34.00 Beef Jin
247 2021-05-14 Whole cattle 34.25 34.25 34.25 Beef Jin
248 2021-05-15 Whole cattle 34.25 34.25 34.25 Beef Jin
249 2021-05-16 Whole cattle 34.25 34.25 34.25 Beef Jin
[221 rows x 7 columns]
Process finished with exit code 0
3, Yes ‘ Whole cattle ’ Draw a line chart of prices
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('vage.csv', header=None)[29:]
# print(data)
# Solve the problem of Chinese garbled code
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = data[0]
y = data[3]
plt.figure(figsize=(12, 8))
plt.plot(x, y, '-r', lw=2, label=' Price ')
plt.legend()
plt.xlabel(' Time / God ', c='b', size=13)
plt.ylabel(' The unit price / element ', c='b', size=13)
plt.title(' Line chart of whole cow price ', c='g', size=20)
plt.ylim(32, 36)
plt.xticks(np.arange(0, 220, 8), rotation=45)
plt.show()

边栏推荐
- Esp32 connects to Alibaba cloud mqtt IOT platform
- sieve of eratosthenes
- maya建模练习
- Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency
- OKA通证权益解析,参与Okaleido生态建设的不二之选
- Typora无法打开提示安装新版本解决办法
- ~4.1 sword finger offer 05. replace spaces
- @Classmethod decorator
- Business analysis report and data visualization report of CDA level1 knowledge point summary
- Brush questions - Luogu -p1075 prime factor decomposition
猜你喜欢

Leetcode202 --- Happy number

Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO

Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation

~4.2 CCF 2021-12-1 sequence query

Mxnet implementation of densenet (dense connection network)

Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders

飞盘局有多快乐?2022年轻人新潮运动报告

Pytest.mark.parameterize and mock use

Mongodb源码部署以及配置

2271. Maximum number of white bricks covered by blanket ●●
随机推荐
NUC980 设置SSH Xshell连接
Acquisition data transmission mode and online monitoring system of wireless acquisition instrument for vibrating wire sensor of engineering instrument
Internal error of LabVIEW
Brush questions - Luogu -p1146 coin flip
飞沃科技IPO过会:年营收11.3亿 湖南文旅与沅澧投资是股东
Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO
[learning record] plt.show() solution to flash back
Wangeditor rich text editor
Experiment the Arduino code of NTP network timing alarm clock with esp32+tm1638
轻松入门自然语言处理系列 12 隐马尔可夫模型
From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part 2)
Xintang nuc980 set DHCP or static IP
word设置粘贴仅保留文本
Mlops column introduction
Lesson of C function without brackets
Leetcode -- addition of four numbers II
Multidimensional pivoting analysis of CDA level1 knowledge points summary
What problems should SEOER pay attention to when baidu searches and attacks pirated websites?
Brush questions - Luogu -p1150 Peter's smoke
Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation