検索
ホームページバックエンド開発Python チュートリアルPython を使用してデータから複数色の線分をプロットする方法

How to Plot Multi-Colored Line Segments from Data Using Python?

データからの複数色の線分のプロット

データ ポイントを線として視覚化するには、matplotlib を使用できます。ここには、それぞれ緯度と経度の座標を表す 2 つのリスト 'latt' と 'lont' があります。目的は、10 点の各セグメントに固有の色を割り当てて、データ ポイントを結ぶ線をプロットすることです。

アプローチ 1: 個別の線プロット

小規模の場合線分の数に応じて、セグメントごとにさまざまな色で個別の線プロットを作成できます。次のコード例は、このアプローチを示しています。

<code class="python">import numpy as np
import matplotlib.pyplot as plt

# Assume the list of latitude and longitude is provided

# Generate uniqueish colors
def uniqueish_color():
    return plt.cm.gist_ncar(np.random.random())

# Create a plot
fig, ax = plt.subplots()

# Iterate through the data in segments of 10
for start, stop in zip(latt[:-1], latt[1:]):
    # Extract coordinates for each segment
    x = latt[start:stop]
    y = lont[start:stop]

    # Plot each segment with a unique color
    ax.plot(x, y, color=uniqueish_color())

# Display the plot
plt.show()</code>

アプローチ 2: 大規模なデータセットのライン コレクション

膨大な数のライン セグメントを含む大規模なデータセットの場合は、Line を使用します。コレクションにより効率が向上します。以下に例を示します。

<code class="python">import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# Prepare the data as a sequence of line segments
segments = np.hstack([latt[:-1], latt[1:]]).reshape(-1, 1, 2)

# Create a plot
fig, ax = plt.subplots()

# Create a LineCollection object
coll = LineCollection(segments, cmap=plt.cm.gist_ncar)

# Assign random colors to the segments
coll.set_array(np.random.random(latt.shape[0]))

# Add the LineCollection to the plot
ax.add_collection(coll)
ax.autoscale_view()

# Display the plot
plt.show()</code>

結論として、どちらのアプローチでも、データ ポイントのさまざまなセグメントに対してさまざまな色で効果的に線をプロットできます。どちらを選択するかは、プロットする線分の数によって異なります。

以上がPython を使用してデータから複数色の線分をプロットする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python:編集と解釈に深く掘り下げますPython:編集と解釈に深く掘り下げますMay 12, 2025 am 12:14 AM

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?May 12, 2025 am 12:09 AM

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

ループ対pythonのループの場合:説明されたキーの違いループ対pythonのループの場合:説明されたキーの違いMay 12, 2025 am 12:08 AM

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

ループのために:実用的なガイドループのために:実用的なガイドMay 12, 2025 am 12:07 AM

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

Python:それは本当に解釈されていますか?神話を暴くPython:それは本当に解釈されていますか?神話を暴くMay 12, 2025 am 12:05 AM

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

同じ要素を持つPython Concatenateリスト同じ要素を持つPython ConcatenateリストMay 11, 2025 am 12:08 AM

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

解釈対編集言語:Pythonの場所解釈対編集言語:Pythonの場所May 11, 2025 am 12:07 AM

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

ループのために:Pythonでそれぞれを使用するのはいつですか?ループのために:Pythonでそれぞれを使用するのはいつですか?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません