首页 后端开发 Python教程 python如何绘制降水图

python如何绘制降水图

Jun 19, 2019 am 10:10 AM
python

python能快速解决日常工作中的小任务,比如数据展示。python做数据展示,主要用到matplotlib库,使用简单的代码,就可以很方便的绘制折线图、柱状图等。使用Java等,可能还需要配合html来进行展示,十分繁琐。

python如何绘制降水图

 各种平面图的绘制代码:

'''
File Name:   draw
Description: 图形绘制。十分有用,对于工作中实验性的项目,可以快速展示效果。如果使用java,还需要配合前端展示。
'''
import matplotlib.pyplot as plt
import numpy as np  # 模块取别名
# 直方图
def draw_hist():
    mu = 100
    sigma = 20
    x = mu + sigma * np.random.randn(20000)  # 样本数量
    plt.hist(x, bins=100, color='green', normed=True)  # bins:显示有几个直方,normed是否对数据进行标准化
    plt._show()
# 条形图
def draw_bar():
    y = [20, 10, 30, 25, 15]  # Y轴数据
    index = np.arange(5)  # X轴数据,也可以是index = [0,5]
    plt.bar(left=index, height=y, color='blue', width=0.5)
    plt.show()
# 折线图
def draw_plot():
    x = np.linspace(-10, 10, 100)  # -10到10,100个点
    y = x ** 3  # x的3次幂
    plt.plot(x, y, linestyle=&#39;--&#39;, color=&#39;orange&#39;, marker=&#39;<&#39;)
    plt.xlabel(&#39;X&#39;)
    plt.ylabel(&#39;Y&#39;)
    plt.show()
# 散点图
def draw_scatter():
    x = np.random.randn(1000)
    y = x + np.random.randn(1000) * 0.5
    plt.scatter(x, y, s=5, marker=&#39;<&#39;)  # s表示面积,marker表示图形
    plt.show()
# 饼状图
def draw_pie():
    labels = &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;  # 4个模块
    fracs = [15, 30, 45, 10]  # 每个模块占比例
    plt.axes(aspect=1)  # 使x、y轴比例相同
    explode = [0, 0.5, 0, 0]  # 突出某一部分区域
    plt.pie(x=fracs, labels=labels, autopct=&#39;%.0f%%&#39;, explode=explode)  # autopct显示百分比
    plt.show()
# 带图例
def draw_with_legend():
    x = np.arange(1, 11, 1)  # x轴坐标,1开始,11结束,步长为1
    plt.plot(x, x * 2)  # 第一条线,x,y坐标
    plt.plot(x, x * 3)
    plt.plot(x, x * 4)
    plt.legend([&#39;Normal&#39;, &#39;Fast&#39;, &#39;Faster&#39;])  # 设置图例,与上面的线对应
    plt.grid(True, color=&#39;green&#39;, linestyle=&#39;--&#39;, linewidth=1)  # 绘制网格
    plt.show()
# start
if __name__ == &#39;__main__&#39;:
    # draw_hist()
    # draw_bar()
    draw_plot()
    # draw_scatter()
    # draw_pie()
    # draw_with_legend()

3D图的绘制代码:

&#39;&#39;&#39;
File Name:   draw_3d
Description: 3D绘图
&#39;&#39;&#39;
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 3D 绘制
def draw_3D():
    fig = plt.figure()  # 定义一个窗口
    ax = Axes3D(fig)  # 绘制3D坐标
    # 设置x、y、z的值
    x = np.arange(-4, 4, 0.25)
    y = np.arange(-4, 4, 0.25)
    x, y = np.meshgrid(x, y)  # x-y 平面的网格
    r = np.sqrt(x ** 2 + y ** 2)
    z = np.sin(r)  # z值
    # 做出一个三维曲面,并将一个 colormap rainbow 填充颜色,之后将三维图像投影到 XY 平面上做一个等高线图
    # rstride 和 cstride 分别代表 row 和 column 的跨度。
    ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=plt.get_cmap(&#39;rainbow&#39;))
    # 添加 XY 平面的等高线
    ax.contourf(x, y, z, zdir=&#39;z&#39;, offset=-2, cmap=plt.get_cmap(&#39;rainbow&#39;))
    ax.set_zlim(-2, 2)
    plt.show()  # 展示
# start
if __name__ == &#39;__main__&#39;:
    draw_3D()

以上是python如何绘制降水图的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

Rimworld Odyssey温度指南和Gravtech
1 个月前 By Jack chen
初学者的Rimworld指南:奥德赛
1 个月前 By Jack chen
PHP变量范围解释了
4 周前 By 百草
撰写PHP评论的提示
3 周前 By 百草
在PHP中评论代码
3 周前 By 百草

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1604
29
PHP教程
1509
276
如何在Python中创建虚拟环境 如何在Python中创建虚拟环境 Aug 05, 2025 pm 01:05 PM

创建Python虚拟环境可使用venv模块,步骤为:1.进入项目目录执行python-mvenvenv创建环境;2.Mac/Linux用sourceenv/bin/activate、Windows用env\Scripts\activate激活;3.使用pipinstall安装包、pipfreeze>requirements.txt导出依赖;4.注意避免将虚拟环境提交到Git,并确认安装时处于正确环境。虚拟环境能隔离项目依赖防止冲突,尤其适合多项目开发,编辑器如PyCharm或VSCode也

如何在Python中的多个过程之间共享数据? 如何在Python中的多个过程之间共享数据? Aug 02, 2025 pm 01:15 PM

使用multiprocessing.Queue可在多个进程间安全传递数据,适合多生产者和消费者的场景;2.使用multiprocessing.Pipe可实现两个进程间的双向高速通信,但仅限两点连接;3.使用Value和Array可在共享内存中存储简单数据类型,需配合Lock避免竞争条件;4.使用Manager可共享复杂数据结构如列表和字典,灵活性高但性能较低,适用于复杂共享状态的场景;应根据数据大小、性能需求和复杂度选择合适方法,Queue和Manager最适合初学者使用。

Python Boto3 S3上传示例 Python Boto3 S3上传示例 Aug 02, 2025 pm 01:08 PM

使用boto3上传文件到S3需先安装boto3并配置AWS凭证;2.通过boto3.client('s3')创建客户端并调用upload_file()方法上传本地文件;3.可指定s3_key作为目标路径,若未指定则使用本地文件名;4.应处理FileNotFoundError、NoCredentialsError和ClientError等异常;5.可通过ExtraArgs参数设置ACL、ContentType、StorageClass和Metadata;6.对于内存数据,可使用BytesIO创建字

如何使用Python中的列表实现堆栈数据结构? 如何使用Python中的列表实现堆栈数据结构? Aug 03, 2025 am 06:45 AM

PythonlistScani ImplementationAking append () Penouspop () Popopoperations.1.UseAppend () Two -Belief StotetopoftHestack.2.UseP OP () ToremoveAndreturnthetop element, EnsuringTocheckiftHestackisnotemptoavoidindexError.3.Pekattehatopelementwithstack [-1] on

Python时间表库示例 Python时间表库示例 Aug 04, 2025 am 10:33 AM

使用Pythonschedule库可轻松实现定时任务,首先通过pipinstallschedule安装库,接着导入schedule和time模块,定义需要定时执行的函数,然后使用schedule.every()设置时间间隔并绑定任务函数,最后通过while循环中调用schedule.run_pending()和time.sleep(1)持续运行任务;例如每10秒执行一次任务可写为schedule.every(10).seconds.do(job),支持按分钟、小时、天、周等周期调度,也可指定具体

如何在崇高文本中运行Python代码 如何在崇高文本中运行Python代码 Aug 04, 2025 pm 04:25 PM

EnsurePythonisinstalledandaddedtoPATHbycheckingversioninterminal;2.Savefilewith.pyextension;3.UseCtrl Btorunviadefaultbuildsystem;4.CreateacustombuildsystemifneededbygoingtoTools>BuildSystem>NewBuildSystem,enteringthecorrectcmdforyourPythonvers

在Python中调试内存泄漏的常见策略是什么? 在Python中调试内存泄漏的常见策略是什么? Aug 06, 2025 pm 01:43 PM

Usetracemalloctotrackmemoryallocationsandidentifyhigh-memorylines;2.Monitorobjectcountswithgcandobjgraphtodetectgrowingobjecttypes;3.Inspectreferencecyclesandlong-livedreferencesusingobjgraph.show_backrefsandcheckforuncollectedcycles;4.Usememory_prof

Python的生存分析 Python的生存分析 Aug 03, 2025 pm 12:58 PM

生存分析用于研究事件发生的时间,Python中常用lifelines和scikit-survival实现。1.安装lifelines库并准备包含时间与事件状态的数据;2.使用Kaplan-Meier估计器绘制生存曲线以可视化事件未发生的概率;3.通过Cox比例风险模型分析变量对事件时间的影响,并检查模型假设;4.注意删失数据的处理,确保event列正确标记删失与事件发生。

See all articles