python3

您所在的位置:网站首页 怎么取消百度云盘自动续费服务 python3

python3

#python3| 来源: 网络整理| 查看: 265

1、什么是matplotlib

Matplotlib 是 Python 中最受欢迎的数据可视化软件包之一,支持跨平台运行,它是 Python 常用的 2D 绘图库,同时它也提供了一部分 3D 绘图接口。Matplotlib 通常与 NumPy、Pandas 一起使用,是数据分析中不可或缺的重要工具之一。Matplotlib 是 Python 中类似 MATLAB 的 绘图工具。MATLAB是美国MathWorks公司出品的商业数学软件,用于数据分析、无线通信、深度学习、图像处理与计算机视觉、信号处理、量化金融与风险管理、机器人,控制系统等领域。matplotlib 支持的图形 https://matplotlib.org/stable/gallery/index.html

2、基础使用(以下都以折线图为例) from matplotlib import pyplot as plt x = range(2,26,2) y = [15,13,14.5,17,20,25,26,26,24,22,18,15] plt.plot(x,y) plt.show()

python3-matplotlib基本使用(以折线图为例)上面图片存在的问题: 图片大小(想要高清大图) [En]

the size of the picture (want a high-definition large image)*

保存到本地描述信息,比如x,y轴表示什么调整刻度的间距线条样式(颜色、透明度)[En]

Line style (color, transparency)*

标注特殊点位(如最高点、最低点)[En]

Mark special points (such as highest point, lowest point)*

给图片加水印,防伪3、设置图片宽高、分辨率、xy轴刻度、保存图片到本地from matplotlib import pyplot as pltx = range(2,26,2)y = [15,13,14.5,17,20,25,26,26,24,22,18,15]"""figure 指的是我们画的图figsize 宽 高dpi 没英寸多少个点,点多了,放大后不会模糊,有锯齿形"""plt.figure(figsize=(20, 8), dpi=80)plt.plot(x,y)xticks_lables = [i/2 for i in range(4, 49)]plt.xticks(xticks_lables[::3])plt.yticks(range(min(y), max(y)+1))plt.show()

python3-matplotlib基本使用(以折线图为例) 4、X,y轴显示字符串并旋转显示

显示10点到12点每分钟气温变化

from matplotlib import pyplot as plt import random x = range(0, 120) y = [random.randint(20, 35) for i in range(120)] plt.figure(figsize=(15, 8), dpi=80) plt.plot(x,y) xtick_lables = ["10:{}".format(i) for i in range(60)] xtick_lables += ["11:{}".format(i) for i in range(60)] plt.xticks(list(x)[::3], xtick_lables[::3],rotation=60) yticks_lables = ["{}tem".format(i) for i in range(15, 40, 2)] plt.yticks(range(15, 40, 2), yticks_lables,rotation=60) plt.show()

python3-matplotlib基本使用(以折线图为例) 5、X,y轴显示中文并设置字体大小

如果未设置,则无法正确显示中文

[En]

If it is not set, the Chinese language cannot be displayed properly

针对Windows ,字体存放位置 C:\Windows\Fontslinux/mac可以参考: https://www.bilibili.com/video/BV1hx411d7jb?p=6也可以参考: https://www.runoob.com/numpy/numpy-matplotlib.html

from matplotlib import pyplot as plt import random from matplotlib import font_manager my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12) x = range(0, 120) y = [random.randint(20, 35) for i in range(120)] plt.figure(figsize=(15, 8), dpi=80) plt.plot(x,y) xtick_lables = ["10:{}".format(i) for i in range(60)] xtick_lables += ["11:{}".format(i) for i in range(60)] plt.xticks(list(x)[::3], xtick_lables[::3],rotation=60) yticks_lables = ["{}度".format(i) for i in range(15, 40, 2)] plt.yticks(range(15, 40, 2), yticks_lables,rotation=60,fontproperties=my_font) plt.show()

python3-matplotlib基本使用(以折线图为例) 6、设置XY轴和标题的描述信息并绘制网格 from matplotlib import pyplot as plt import random from matplotlib import font_manager my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12) x = range(0, 120) y = [random.randint(20, 35) for i in range(120)] plt.figure(figsize=(15, 8), dpi=80) plt.plot(x,y) xtick_lables = ["10:{}".format(i) for i in range(60)] xtick_lables += ["11:{}".format(i) for i in range(60)] plt.xticks(list(x)[::3], xtick_lables[::3],rotation=60) yticks_lables = ["{}度".format(i) for i in range(15, 40, 2)] plt.yticks(range(15, 40, 2), yticks_lables,rotation=60,fontproperties=my_font) plt.xlabel("时间",fontproperties=my_font) plt.ylabel("温度 单位(℃)",fontproperties=my_font) plt.title("10点到12点每分钟的气温变化情况",fontproperties=my_font) plt.grid(alpha=0.9) plt.show()

python3-matplotlib基本使用(以折线图为例) 7、同时绘制出两条折线并添加图例、设置折线颜色,折线样式 from matplotlib import pyplot as plt from matplotlib import font_manager """ a 是自己 b 是同桌 x 轴是年龄 y 轴是女朋友的数量 """ a = [1,0,2,1,5,2,3,6,9,5,1,2,3,1,2,3,2,1,2,1] b = [1,3,1,1,2,1,1,1,4,4,4,2,2,2,4,1,1,4,1,2] x = range(11, 31) my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12) plt.figure(figsize=(15, 8), dpi=80) plt.plot(x, a, label="自己", color="orange", linestyle="--", linewidth=3, alpha=0.3) plt.plot(x, b, label="同桌", color="#FF69B4", linestyle="-.",linewidth=6, alpha=0.6) xtick_labels = ["{}岁".format(i) for i in x] plt.xticks(x, xtick_labels, fontproperties=my_font) plt.yticks(range(0,11)) plt.grid(alpha=0.6, linestyle="--") plt.legend(prop=my_font,loc="upper left") plt.show()

python3-matplotlib基本使用(以折线图为例) 8、标记最高(低)点,添加水印

其他 图像添加image水印、图像添加背景 可 参考 https://zhuanlan.zhihu.com/p/390880393

from matplotlib import pyplot as plt from matplotlib import font_manager """ a 是自己 b 是同桌 x 轴是年龄 y 轴是女朋友的数量 """ a = [1,0,2,1,5,2,3,6,9,5,1,2,3,1,2,3,2,1,2,1] b = [1,3,1,1,2,1,1,1,4,4,4,2,2,2,4,1,1,4,1,2] x = range(11, 31) my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12) plt.figure(figsize=(15, 8), dpi=80) plt.plot(x, a, label="自己", color="orange", linestyle="--", linewidth=3, alpha=0.3) plt.plot(x, b, label="同桌", color="#FF69B4", linestyle="-.",linewidth=6, alpha=0.6) xtick_labels = ["{}岁".format(i) for i in x] plt.xticks(x, xtick_labels, fontproperties=my_font) plt.yticks(range(0,11)) plt.scatter(x[8], a[8], color='b', marker='o', edgecolors='r', s=300) plt.text( x=15, y=5, s='hello world', rotation=15, ha='center', va= 'center', alpha=0.5, fontdict=dict( fontsize=32, color='grey', family= 'monospace', weight= 'light', ) ) plt.grid(alpha=0.6, linestyle="--") plt.legend(prop=my_font,loc="upper left") plt.show()

python3-matplotlib基本使用(以折线图为例)

https://www.bilibili.com/video/BV1hx411d7jb?p=3https://www.bilibili.com/video/BV1hx411d7jb?p=4https://www.bilibili.com/video/BV1hx411d7jb?p=5https://www.bilibili.com/video/BV1hx411d7jb?p=6 设置中文显示https://www.bilibili.com/video/BV1hx411d7jb?p=7https://www.bilibili.com/video/BV1hx411d7jb?p=8

Original: https://blog.csdn.net/qq_38959934/article/details/121086376Author: 王2gou蛋Title: python3-matplotlib基本使用(以折线图为例)

相关阅读 Title: 怎样使用TUShare把股票数据存入csv文件

(原内容:)

直接看程序吧:

通过ts读取数据到df,存入csv文件,再读出来

import pandas as pdimport tushare as ts

from pandas import DataFrame

读取ts数据

df = ts.get_k_data(‘sh600519’, ‘1980-01-01’) # 当然,它是2001年上市的print(‘df:\n’, df)

写入csv

df.to_csv(‘sh600519.csv’)

原汁原味读出

df1 = pd.read_csv(‘sh600519.csv’)

用date作索引,并把它由字符串转为date对象

df2 = pd.read_csv(‘sh600519.csv’, index_col = ‘date’,parse_dates=[‘date’])

在df2的基础上,只读取指定的列

df3 = pd.read_csv(‘sh600519.csv’, index_col = ‘date’,parse_dates=[‘date’])[[‘open’,’close’,’high’,’low’,’volume’,’code’]]

看一看读的效果

print(‘df1:\n’, df1)print(‘df2:\n’, df2)print(‘df3:\n’, df3)

运行结果:

df:date open close high low volume code0 2001-08-27 5.392 5.554 5.902 5.132 406318.00 sh6005191 2001-08-28 5.467 5.759 5.781 5.407 129647.79 sh6005192 2001-08-29 5.777 5.684 5.781 5.640 53252.75 sh6005193 2001-08-30 5.668 5.796 5.860 5.624 48013.06 sh6005194 2001-08-31 5.804 5.782 5.877 5.749 23231.48 sh600519… … … … … … … …

4452 2020-04-24 1248.000 1250.560 1259.890 1235.180 19122.00 sh6005194453 2020-04-27 1257.000 1276.000 1278.170 1250.960 25904.00 sh6005194454 2020-04-28 1285.310 1279.130 1299.940 1271.880 34662.00 sh6005194455 2020-04-29 1277.800 1274.900 1288.100 1258.000 23444.00 sh6005194456 2020-04-30 1271.000 1265.700 1285.010 1258.880 24661.00 sh600519

[4457 rows x 7 columns]df1:Unnamed: 0 date open … low volume code0 0 2001-08-27 5.392 … 5.132 406318.00 sh6005191 1 2001-08-28 5.467 … 5.407 129647.79 sh6005192 2 2001-08-29 5.777 … 5.640 53252.75 sh6005193 3 2001-08-30 5.668 … 5.624 48013.06 sh6005194 4 2001-08-31 5.804 … 5.749 23231.48 sh600519… … … … … … … …

4452 4452 2020-04-24 1248.000 … 1235.180 19122.00 sh6005194453 4453 2020-04-27 1257.000 … 1250.960 25904.00 sh6005194454 4454 2020-04-28 1285.310 … 1271.880 34662.00 sh6005194455 4455 2020-04-29 1277.800 … 1258.000 23444.00 sh6005194456 4456 2020-04-30 1271.000 … 1258.880 24661.00 sh600519

[4457 rows x 8 columns]df2:Unnamed: 0 open close … low volume codedate …

2001-08-27 0 5.392 5.554 … 5.132 406318.00 sh6005192001-08-28 1 5.467 5.759 … 5.407 129647.79 sh6005192001-08-29 2 5.777 5.684 … 5.640 53252.75 sh6005192001-08-30 3 5.668 5.796 … 5.624 48013.06 sh6005192001-08-31 4 5.804 5.782 … 5.749 23231.48 sh600519… … … … … … … …

2020-04-24 4452 1248.000 1250.560 … 1235.180 19122.00 sh6005192020-04-27 4453 1257.000 1276.000 … 1250.960 25904.00 sh6005192020-04-28 4454 1285.310 1279.130 … 1271.880 34662.00 sh6005192020-04-29 4455 1277.800 1274.900 … 1258.000 23444.00 sh6005192020-04-30 4456 1271.000 1265.700 … 1258.880 24661.00 sh600519

[4457 rows x 7 columns]df3:open close high low volume codedate2001-08-27 5.392 5.554 5.902 5.132 406318.00 sh6005192001-08-28 5.467 5.759 5.781 5.407 129647.79 sh6005192001-08-29 5.777 5.684 5.781 5.640 53252.75 sh6005192001-08-30 5.668 5.796 5.860 5.624 48013.06 sh6005192001-08-31 5.804 5.782 5.877 5.749 23231.48 sh600519… … … … … … …

2020-04-24 1248.000 1250.560 1259.890 1235.180 19122.00 sh6005192020-04-27 1257.000 1276.000 1278.170 1250.960 25904.00 sh6005192020-04-28 1285.310 1279.130 1299.940 1271.880 34662.00 sh6005192020-04-29 1277.800 1274.900 1288.100 1258.000 23444.00 sh6005192020-04-30 1271.000 1265.700 1285.010 1258.880 24661.00 sh600519

[4457 rows x 6 columns]

Original: https://blog.csdn.net/shclbear/article/details/117113084Author: 熊出没883Title: 怎样使用TUShare把股票数据存入csv文件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/327039/

转载文章受原作者版权保护。转载请注明原作者出处!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3