欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Matplotlib繪制不同顏色的帶箭頭的線實例

 更新時間:2020年04月17日 10:27:58   作者:月落烏啼silence  
這篇文章主要介紹了使用Matplotlib繪制不同顏色的帶箭頭的線實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

周五的時候計算出來一條線路,但是計算出來的只是類似與

0->10->19->2->..0

這樣的線路只有寫代碼的人才能看的懂無法直觀的表達出來,讓其它同事看的不清晰,所以考慮怎樣直觀的把線路圖畫出來。

&esp; 當(dāng)然是考慮用matplotlib了,

導(dǎo)入相關(guān)的庫

import matplotlib.pyplot as plt
import numpy
import matplotlib.colors as colors
import matplotlib.cm as cmx

后面兩個主要是用于處理顏色的。

準備數(shù)據(jù)

 _locations = [
    (4, 4), # depot
    (4, 4), # unload depot_prime
    (4, 4), # unload depot_second
    (4, 4), # unload depot_fourth
    (4, 4), # unload depot_fourth
    (4, 4), # unload depot_fifth
    (2, 0),
    (8, 0), # locations to visit
    (0, 1),
    (1, 1),
    (5, 2),
    (7, 2),
    (3, 3),
    (6, 3),
    (5, 5),
    (8, 5),
    (1, 6),
    (2, 6),
    (3, 7),
    (6, 7),
    (0, 8),
    (7, 8)
  ]

畫圖

plt.figure(figsize=(10, 10))
p1 = [l[0] for l in _locations]
p2 = [l[1] for l in _locations]
plt.plot(p1[:6], p2[:6], 'g*', ms=20, label='depot')
plt.plot(p1[6:], p2[6:], 'ro', ms=15, label='customer')
plt.grid(True)
plt.legend(loc='lower left')

way = [[0, 12, 18, 17, 16, 4, 14, 10, 11, 13, 5], [0, 6, 9, 8, 20, 3], [0, 19, 21, 15, 7, 2]]  # 

cmap = plt.cm.jet
cNorm = colors.Normalize(vmin=0, vmax=len(way))
scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=cmap)

for k in range(0, len(way)):
  way0 = way[k]
  colorVal = scalarMap.to_rgba(k)
  for i in range(0, len(way0)-1):
    start = _locations[way0[i]]
    end = _locations[way0[i+1]]
#     plt.arrow(start[0], start[1], end[0]-start[0], end[1]-start[1], length_includes_head=True,
#         head_width=0.2, head_length=0.3, fc='k', ec='k', lw=2, ls=lineStyle[k], color='red')
    plt.arrow(start[0], start[1], end[0]-start[0], end[1]-start[1], 
         length_includes_head=True, head_width=0.2, lw=2,
         color=colorVal)
plt.show()
cmap = plt.cm.jet
cNorm = colors.Normalize(vmin=0, vmax=len(way))
scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=cmap)

cmap可以理解為顏色庫,cNorm設(shè)置顏色的范圍,有幾條線路就設(shè)置幾種顏色,scalarMap顏色生成完畢。最后在繪圖的時候,根據(jù)索引獲得相應(yīng)的顏色就可以了。

結(jié)果如下:

補充知識:Python包matplotlib繪圖--如何標(biāo)注某點--附代碼

# -*- coding: utf-8 -*-
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('classic')

plt.rcParams['font.sans-serif'] = ['SimHei'] #解決中文顯示
plt.rcParams['axes.unicode_minus'] = False #解決符號無法顯示

x=np.array([1,2,3,4,5,6,7,8])
y1=np.array([3,5,35,300,800,600,1200,4000])
y2=np.array([8,14,94,703,1300,1660,2801,12768])

fig1 = plt.figure()

ax = plt.axes()
ax.plot(x, y2,label='時間/秒')
ax.set(xlabel='目標(biāo)函數(shù)個數(shù)', ylabel='程序運行時間',title='多目標(biāo)收斂速度')

plt.hlines(703, 0, 4, colors='r', linestyle="--")
plt.text(0, 703, "703")
plt.hlines(1300, 0, 5, colors='g', linestyle="--")
plt.text(0, 1300, "1300")

# annotate 
plt.annotate("703秒", (4,703), xycoords='data',
       xytext=(4.2, 2000), 
       arrowprops=dict(arrowstyle='->')) 
plt.annotate("94秒", (3,94), xycoords='data',
       xytext=(3.5, 2000), 
       arrowprops=dict(arrowstyle='->')) 
plt.annotate("14秒", (2,14), xycoords='data',
       xytext=(2.5, 2000), 
       arrowprops=dict(arrowstyle='->')) 
plt.annotate("8秒", (1,8), xycoords='data',
       xytext=(1.5, 2000), 
       arrowprops=dict(arrowstyle='->')) 
plt.legend()
plt.show()
fig1.savefig('my_figure1.png')

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Use seaborn to change the default graphics to something nicer
import seaborn as sns
# And set a nice color palette
sns.set_color_codes('deep')

# Create the plot object
fig, ax = plt.subplots(figsize=(5, 4))
x = np.linspace(0, 1000)

# Add finishing constraint: x2 <= 100/2 - x1/2
plt.plot(x, 50/4 - 3*x/4, linewidth=3, label='First constraint')
plt.fill_between(x, 0, 100/2 - x/2, alpha=0.1)

# Add carpentry constraint: x2 <= 80 - x1
plt.plot(x, 30 - 2*x, linewidth=3, label='Second constraint')
plt.fill_between(x, 0, 100 - 2*x, alpha=0.1)

# Add non-negativity constraints
plt.plot(np.zeros_like(x), x, linewidth=3, label='$x$ Sign restriction')
plt.plot(x, np.zeros_like(x), linewidth=3, label='$y$ Sign restriction')

#====================================================
# This part is different from giapetto_feasible.py
# Plot the possible (x1, x2) pairs
pairs = [(x, y) for x in np.arange(101)
        for y in np.arange(101)
        if (300*x + 400*y) <= 5000
        and (200*x + 100*y) <= 3000]

# Split these into our variables
chairs, tables = np.hsplit(np.array(pairs), 2)

# Caculate the objective function at each pair
z =8*chairs + 9*tables

# Plot the results
plt.scatter(chairs, tables, c=z, cmap='jet', edgecolor='gray', alpha=0.5, label='Profit at each point', zorder=3)

# Colorbar
cb = plt.colorbar()
cb.set_label('Profit Colormap ($)')
#====================================================

# Labels and stuff
plt.xlabel('Package A')
plt.ylabel('Package B')
plt.xlim(-0.5, 20)
plt.ylim(-0.5, 20)
plt.legend()
fig01 = plt.figure()
plt.show()

以上這篇使用Matplotlib繪制不同顏色的帶箭頭的線實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python使用numpy讀取、保存txt數(shù)據(jù)的實例

    python使用numpy讀取、保存txt數(shù)據(jù)的實例

    今天小編就為大家分享一篇python使用numpy讀取、保存txt數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出示例

    Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出示例

    這篇文章主要介紹了Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出,涉及Python文件讀取、字符串替換、分割以及字典遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • python 統(tǒng)計文件中的字符串?dāng)?shù)目示例

    python 統(tǒng)計文件中的字符串?dāng)?shù)目示例

    今天小編就為大家分享一篇python 統(tǒng)計文件中的字符串?dāng)?shù)目示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 解決 jupyter notebook 回車換兩行問題

    解決 jupyter notebook 回車換兩行問題

    這篇文章主要介紹了解決 jupyter notebook 回車換兩行問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • pycocotools介紹以及在windows10下的安裝過程

    pycocotools介紹以及在windows10下的安裝過程

    這篇文章主要介紹了pycocotools介紹以及在windows10下的安裝過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python?中的反轉(zhuǎn)字符串reversed(),切片

    Python?中的反轉(zhuǎn)字符串reversed(),切片

    這篇文章主要介紹了Python?中的反轉(zhuǎn)字符串reversed(),切片?,以相反的順序反轉(zhuǎn)和處理字符串可能是編程中的一項常見任務(wù)。Python?提供了一組工具和技術(shù),可以幫助我們快速有效地執(zhí)行字符串反轉(zhuǎn),下面來看看具體內(nèi)容吧
    2021-12-12
  • 解決Python調(diào)用df.to_csv()出現(xiàn)中文亂碼的問題

    解決Python調(diào)用df.to_csv()出現(xiàn)中文亂碼的問題

    在Python使用df.to_csv()時,若出現(xiàn)中文亂碼,可通過加入?yún)?shù)encoding="utf_8_sig"解決,"utf-8"編碼不包含BOM,直接處理文件時會將BOM誤讀為內(nèi)容;而"utf_8_sig"會識別并處理BOM,避免亂碼,此方法為實踐經(jīng)驗,供參考
    2024-09-09
  • Python圖算法實例分析

    Python圖算法實例分析

    這篇文章主要介紹了Python圖算法,結(jié)合實例形式詳細分析了Python數(shù)據(jù)結(jié)構(gòu)與算法中的圖算法實現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08
  • Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實戰(zhàn)

    Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實戰(zhàn)

    這篇文章主要介紹了Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實戰(zhàn),python在網(wǎng)絡(luò)方面封裝一些內(nèi)置模塊,可以用很簡潔的代碼實現(xiàn)端到端的通信,比如HTTP、RPC服務(wù),下文實戰(zhàn)詳情,需要的朋友可以參考一下
    2022-09-09
  • python中csv文件數(shù)據(jù)顏色設(shè)置方式

    python中csv文件數(shù)據(jù)顏色設(shè)置方式

    這篇文章主要介紹了python中csv文件數(shù)據(jù)顏色設(shè)置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論