如何为 10 个连续点的每段绘制不同颜色的线?
以不同颜色绘制一条线
问题陈述
给定两个列表,latt 和 lont,目标是绘制一条线,其中每个列表10个连续点的线段用不同的颜色表示。
解决方案
线段数量有限
如果线段数量很少,例如 10 个或更少,一个简单的方法是使用循环以唯一的颜色绘制每个线段。
<code class="python">import numpy as np import matplotlib.pyplot as plt # Generate random colors def uniqueish_color(): return plt.cm.gist_ncar(np.random.random()) # Plot the line segments xy = (np.random.random((10, 2)) - 0.5).cumsum(axis=0) fig, ax = plt.subplots() for start, stop in zip(xy[:-1], xy[1:]): x, y = zip(start, stop) ax.plot(x, y, color=uniqueish_color()) plt.show()</code>
大量线段
对于大量线段,使用循环可能会很慢。相反,创建一个 LineCollection 对象。
<code class="python">import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection # Generate the line segments xy = (np.random.random((1000, 2)) - 0.5).cumsum(axis=0) xy = xy.reshape(-1, 1, 2) segments = np.hstack([xy[:-1], xy[1:]]) # Create a LineCollection object fig, ax = plt.subplots() coll = LineCollection(segments, cmap=plt.cm.gist_ncar) # Set the color array coll.set_array(np.random.random(xy.shape[0])) # Add the LineCollection to the axes ax.add_collection(coll) ax.autoscale_view() # Display the plot plt.show()</code>
选择颜色
对于这两种方法,我们使用“gist_ncar”颜色图来生成唯一的颜色。有关其他颜色图选项,请参阅此页面:http://matplotlib.org/examples/color/colormaps_reference.html
以上是如何为 10 个连续点的每段绘制不同颜色的线?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
