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

python中matplotlib的顏色以及形狀實(shí)例詳解

 更新時間:2022年01月24日 11:39:32   作者:Mortal71  
在Python中經(jīng)常使用matplotlib畫圖,為了讓圖像顯示的更加好看,經(jīng)常需要對圖表點(diǎn)、線形狀及顏色進(jìn)行設(shè)置,下面這篇文章主要給大家介紹了關(guān)于python中matplotlib的顏色以及形狀的相關(guān)資料,需要的朋友可以參考下

繪制折線圖

命令形如:

# 常用
plt.plot(x, y, linewidth = '1', label = "test", color=' red ', linestyle=':', marker='|')

# 所有可選參數(shù)
plt.plot(x,y,color,linestyle=,linewidth,marker,markeredgecolor,markeredgwidth,markerfacecolor,markersize,label)

plt.legend(loc='upper left')
plt.show()

主要參數(shù)詳解:

線條形式(linestyle):

標(biāo)記字符還可使用說明
‘-’“solid”實(shí)線
‘–’“dashed”破折線
‘-.’“dashdot”點(diǎn)劃線
‘:’“dotted”虛線
’ ’‘none’無線條

標(biāo)注形狀(marker):

標(biāo)記字符還可使用說明
‘.’point marker點(diǎn)標(biāo)記
‘,’pixel marker像素標(biāo)記(極小點(diǎn))
‘o’circle marker實(shí)心圈標(biāo)記
‘v’triangle_down marker倒三角標(biāo)記
‘^’triangle_up marker上三角標(biāo)記
‘<’triangle_left marker左三角標(biāo)記
‘>’triangle_right marker右三角標(biāo)記
‘1’tri_down marker下花三角標(biāo)記
‘2’tri_up marker上花三角標(biāo)記
‘3’tri_left marker左花三角標(biāo)記
‘4’tri_right marker右花三角標(biāo)記
‘s’square marker實(shí)心方形標(biāo)記
‘p’pentagon marker實(shí)心五角標(biāo)記
‘*’star marker星形標(biāo)記
‘h’hexagon1 marker豎六邊形標(biāo)記
‘H’hexagon2 marker橫六邊形標(biāo)記
‘+’plus marker十字標(biāo)記
‘x’x markerx標(biāo)記
‘D’diamond marker菱形標(biāo)記
‘d’thin_diamond marker受菱形標(biāo)記
‘|’vline marker垂直線標(biāo)記
‘_’hline marker水平線標(biāo)記

顏色(color),可用十六進(jìn)制形式,每兩個十六進(jìn)制數(shù)分別代表R、G、B分量,可用如下代碼展示所有:

import matplotlib
for name, hex in matplotlib.colors.cnames.items():
    print(name, hex)

得所有支持顏色:

 cnames = {
    'aliceblue':            '#F0F8FF',
    'antiquewhite':         '#FAEBD7',
    'aqua':                 '#00FFFF',
    'aquamarine':           '#7FFFD4',
    'azure':                '#F0FFFF',
    'beige':                '#F5F5DC',
    'bisque':               '#FFE4C4',
    'black':                '#000000',
    'blanchedalmond':       '#FFEBCD',
    'blue':                 '#0000FF',
    'blueviolet':           '#8A2BE2',
    'brown':                '#A52A2A',
    'burlywood':            '#DEB887',
    'cadetblue':            '#5F9EA0',
    'chartreuse':           '#7FFF00',
    'chocolate':            '#D2691E',
    'coral':                '#FF7F50',
    'cornflowerblue':       '#6495ED',
    'cornsilk':             '#FFF8DC',
    'crimson':              '#DC143C',
    'cyan':                 '#00FFFF',
    'darkblue':             '#00008B',
    'darkcyan':             '#008B8B',
    'darkgoldenrod':        '#B8860B',
    'darkgray':             '#A9A9A9',
    'darkgreen':            '#006400',
    'darkkhaki':            '#BDB76B',
    'darkmagenta':          '#8B008B',
    'darkolivegreen':       '#556B2F',
    'darkorange':           '#FF8C00',
    'darkorchid':           '#9932CC',
    'darkred':              '#8B0000',
    'darksalmon':           '#E9967A',
    'darkseagreen':         '#8FBC8F',
    'darkslateblue':        '#483D8B',
    'darkslategray':        '#2F4F4F',
    'darkturquoise':        '#00CED1',
    'darkviolet':           '#9400D3',
    'deeppink':             '#FF1493',
    'deepskyblue':          '#00BFFF',
    'dimgray':              '#696969',
    'dodgerblue':           '#1E90FF',
    'firebrick':            '#B22222',
    'floralwhite':          '#FFFAF0',
    'forestgreen':          '#228B22',
    'fuchsia':              '#FF00FF',
    'gainsboro':            '#DCDCDC',
    'ghostwhite':           '#F8F8FF',
    'gold':                 '#FFD700',
    'goldenrod':            '#DAA520',
    'gray':                 '#808080',
    'green':                '#008000',
    'greenyellow':          '#ADFF2F',
    'honeydew':             '#F0FFF0',
    'hotpink':              '#FF69B4',
    'indianred':            '#CD5C5C',
    'indigo':               '#4B0082',
    'ivory':                '#FFFFF0',
    'khaki':                '#F0E68C',
    'lavender':             '#E6E6FA',
    'lavenderblush':        '#FFF0F5',
    'lawngreen':            '#7CFC00',
    'lemonchiffon':         '#FFFACD',
    'lightblue':            '#ADD8E6',
    'lightcoral':           '#F08080',
    'lightcyan':            '#E0FFFF',
    'lightgoldenrodyellow': '#FAFAD2',
    'lightgreen':           '#90EE90',
    'lightgray':            '#D3D3D3',
    'lightpink':            '#FFB6C1',
    'lightsalmon':          '#FFA07A',
    'lightseagreen':        '#20B2AA',
    'lightskyblue':         '#87CEFA',
    'lightslategray':       '#778899',
    'lightsteelblue':       '#B0C4DE',
    'lightyellow':          '#FFFFE0',
    'lime':                 '#00FF00',
    'limegreen':            '#32CD32',
    'linen':                '#FAF0E6',
    'magenta':              '#FF00FF',
    'maroon':               '#800000',
    'mediumaquamarine':     '#66CDAA',
    'mediumblue':           '#0000CD',
    'mediumorchid':         '#BA55D3',
    'mediumpurple':         '#9370DB',
    'mediumseagreen':       '#3CB371',
    'mediumslateblue':      '#7B68EE',
    'mediumspringgreen':    '#00FA9A',
    'mediumturquoise':      '#48D1CC',
    'mediumvioletred':      '#C71585',
    'midnightblue':         '#191970',
    'mintcream':            '#F5FFFA',
    'mistyrose':            '#FFE4E1',
    'moccasin':             '#FFE4B5',
    'navajowhite':          '#FFDEAD',
    'navy':                 '#000080',
    'oldlace':              '#FDF5E6',
    'olive':                '#808000',
    'olivedrab':            '#6B8E23',
    'orange':               '#FFA500',
    'orangered':            '#FF4500',
    'orchid':               '#DA70D6',
    'palegoldenrod':        '#EEE8AA',
    'palegreen':            '#98FB98',
    'paleturquoise':        '#AFEEEE',
    'palevioletred':        '#DB7093',
    'papayawhip':           '#FFEFD5',
    'peachpuff':            '#FFDAB9',
    'peru':                 '#CD853F',
    'pink':                 '#FFC0CB',
    'plum':                 '#DDA0DD',
    'powderblue':           '#B0E0E6',
    'purple':               '#800080',
    'red':                  '#FF0000',
    'rosybrown':            '#BC8F8F',
    'royalblue':            '#4169E1',
    'saddlebrown':          '#8B4513',
    'salmon':               '#FA8072',
    'sandybrown':           '#FAA460',
    'seagreen':             '#2E8B57',
    'seashell':             '#FFF5EE',
    'sienna':               '#A0522D',
    'silver':               '#C0C0C0',
    'skyblue':              '#87CEEB',
    'slateblue':            '#6A5ACD',
    'slategray':            '#708090',
    'snow':                 '#FFFAFA',
    'springgreen':          '#00FF7F',
    'steelblue':            '#4682B4',
    'tan':                  '#D2B48C',
    'teal':                 '#008080',
    'thistle':              '#D8BFD8',
    'tomato':               '#FF6347',
    'turquoise':            '#40E0D0',
    'violet':               '#EE82EE',
    'wheat':                '#F5DEB3',
    'white':                '#FFFFFF',
    'whitesmoke':           '#F5F5F5',
    'yellow':               '#FFFF00',
    'yellowgreen':          '#9ACD32'}

可用如下代碼展示具體顏色:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
import math

fig = plt.figure()
ax = fig.add_subplot(111)

ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count

for c in colors.cnames:
    pos = (x / x_count, y / y_count)
    ax.add_patch(patches.Rectangle(pos, w, h, color=c))
    ax.annotate(c, xy=pos)
    if y >= y_count-1:
        x += 1
        y = 0
    else:
        y += 1

plt.show()

得下圖

繪制柱形圖

plot.bar(x,height,width=0.8,bottom=None,align='center',color,edgecolor)
參數(shù)說明
x表示在什么位置顯示柱形圖
height柱子高度
width每根柱子的寬度,可各不相同
bottom每根柱子的底部位置,可各不相同
align柱子的位置與x值的關(guān)系,可選center、edge兩個參數(shù),center表示柱子位于x值的中心位置,edge表示邊緣位置
color柱子顏色
edgecolor柱子邊緣的顏色

例:

plt.subplot(1,1,1)

x = np.array(["東區(qū)","西區(qū)","南區(qū)","北區(qū)"])
y = np.array([8566,6482,5335,7310])

plt.bar(x,y,width=0.5,align="center",label="任務(wù)量")

plt.title("全國各分區(qū)任務(wù)量",loc="center")

# 添加數(shù)據(jù)標(biāo)簽
for a,b in zip(x,y):
    plt.text(a,b,b,ha='center',va="bottom",fontsize=12,color="r")
    
plt.xlabel('分區(qū)')
plt.ylabel('任務(wù)量')

plt.legend()     #顯示圖例

#保存到本地
#plt.savefig("C:/Users/.../1.jpg")

簇狀柱形圖

plt.subplot(1,1,1)

x = np.array([1,2,3,4])
y1 = np.array([8566,6482,5335,7310])
y2 = np.array([4283,2667,3655,3241])

plt.bar(x,y1,width=0.3,label="任務(wù)量")
plt.bar(x+0.3,y2,width=0.3,label="完成量")   #x+0.3相當(dāng)于完成量的每個柱子右移0.3

plt.title("全國各分區(qū)任務(wù)量",loc="center")

# 添加數(shù)據(jù)標(biāo)簽
for a,b in zip(x,y1):
    plt.text(a,b,b,ha='center',va="bottom",fontsize=12,color="blue")
    
    
for a,b in zip(x,y2):
    plt.text(a,b,b,ha='center',va="bottom",fontsize=12,color="g")
    
plt.xlabel('區(qū)域')
plt.ylabel('任務(wù)情況')

#設(shè)置x軸刻度值
plt.xticks(x+0.15,["東區(qū)","西區(qū)","南區(qū)","北區(qū)"])

plt.grid(False)
plt.legend()     #顯示圖例

堆積柱形圖

plt.subplot(1,1,1)

x = np.array(["東區(qū)","西區(qū)","南區(qū)","北區(qū)"])
y1 = np.array([8566,6482,5335,7310])
y2 = np.array([4283,2667,3655,3241])

plt.bar(x,y1,width=0.3,label="任務(wù)量")
plt.bar(x,y2,width=0.3,label="完成量")   

plt.title("全國各分區(qū)任務(wù)量",loc="center")

# 添加數(shù)據(jù)標(biāo)簽
for a,b in zip(x,y1):
    plt.text(a,b,b,ha='center',va="bottom",fontsize=12,color="blue")
    
    
for a,b in zip(x,y2):
    plt.text(a,b,b,ha='center',va="bottom",fontsize=12,color="g")
    
plt.xlabel('區(qū)域')
plt.ylabel('任務(wù)情況')


plt.grid(False)

plt.legend(loc = "upper center",ncol=2)     

散點(diǎn)圖

plt.scatter(x,y,s,c,marker,linewidths,edgecolors)
參數(shù)說明
(x,y)散點(diǎn)的位置
s每個點(diǎn)的面積,即散點(diǎn)的大小。若只有一個具體值時,則所有點(diǎn)的大小都一樣。也可呈現(xiàn)多個值,這樣就成了氣泡圖
c每個點(diǎn)的顏色,可多樣
marker標(biāo)記,同折線圖中marker
linewidths散點(diǎn)線寬
edgecolors散點(diǎn)外輪廓的顏色

 

colors = y*10
area = y*100        #根據(jù)y值的大小生成不同形狀

plt.scatter(x,y,c=colors,marker="o",s=area)

plt.title("銷量關(guān)系圖",loc="center")

# 添加數(shù)據(jù)標(biāo)簽
for a,b in zip(x,y):
    plt.text(a,b,b,ha='center',va="center",fontsize=10,color="white")
    
plt.xlabel('氣溫')
plt.ylabel('啤酒銷量')

plt.grid(False)

附:matplotlib實(shí)現(xiàn)區(qū)域顏色填充

'''
學(xué)習(xí)python
'''
import matplotlib.pyplot as plt
import numpy as np
  
x= np.linspace(0,5*np.pi, 1000)
  
y1 = np.sin(x)
y2 = np.sin(2*x)
  
#plt.plot(x,y1)
#plt.plot(x,y2)
  
plt.fill(x,y1,'b',alpha=0.5)
plt.fill(x,y2,'r',alpha=0.3)
  
plt.fill_between(x,y1,y2,facecolor='green')
plt.grid(True)
  
plt.show()
  
#########################################################
plt.plot(x,y1,'b',alpha=0.5)
plt.plot(x,y2,'r',alpha=0.3)
#添加條件
#如果數(shù)據(jù)點(diǎn)比較少的情況下,會有縫隙出現(xiàn),使用interpolate可以填充縫隙
plt.fill_between(x,y1,y2,where=y1>=y2,facecolor='green',interpolate=True)
plt.fill_between(x,y1,y2,where=y2>y1,facecolor='yellow',interpolate=True)
plt.grid(True)
  
plt.show()
###########################################################
  
n = 256
X = np.linspace(-np.pi, np.pi, n, endpoint=True)
Y = np.sin(2 * X)
  
  
plt.plot(X, Y + 1, color='blue', alpha=1.00)
plt.fill_between(X, 1, Y + 1, color='blue', alpha=.25)
  
plt.plot(X, Y - 1, color='blue', alpha=1.00)
plt.fill_between(X, -1, Y - 1, (Y - 1) > -1, color='blue', alpha=.25)
plt.fill_between(X, -1, Y - 1, (Y - 1) < -1, color='red', alpha=.25)
  
plt.xlim(-np.pi, np.pi)
plt.xticks(())
plt.ylim(-2.5, 2.5)
plt.yticks(())

總結(jié) 

到此這篇關(guān)于python中matplotlib的顏色以及形狀的文章就介紹到這了,更多相關(guān)python matplotlib顏色及形狀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python批量自動修改文件名,按指定的格式自動命名方式

    Python批量自動修改文件名,按指定的格式自動命名方式

    這篇文章主要介紹了Python批量自動修改文件名,按指定的格式自動命名方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python WEB應(yīng)用部署的實(shí)現(xiàn)方法

    Python WEB應(yīng)用部署的實(shí)現(xiàn)方法

    這篇文章主要介紹了Python WEB應(yīng)用部署的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • python excel多行合并的方法

    python excel多行合并的方法

    這篇文章主要介紹了python excel多行合并的方法,幫助大家更好的利用python處理excel表格,感興趣的朋友可以了解下
    2020-12-12
  • Selenium中的option使用示例

    Selenium中的option使用示例

    這篇文章主要介紹了Selenium中的option用法實(shí)例,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Python中字符串格式化的方法小結(jié)

    Python中字符串格式化的方法小結(jié)

    在Python中,格式化字符串輸出是一項(xiàng)非常常見的任務(wù),Python提供了多種方式來實(shí)現(xiàn)字符串格式化,每種方式都有其獨(dú)特的優(yōu)勢和用法,下面我們就來學(xué)習(xí)一下這些方法的具體操作吧
    2023-11-11
  • Python中卷積神經(jīng)網(wǎng)絡(luò)(CNN)入門教程分分享

    Python中卷積神經(jīng)網(wǎng)絡(luò)(CNN)入門教程分分享

    卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)是一類特別適用于處理圖像數(shù)據(jù)的深度學(xué)習(xí)模型,本文介紹了如何使用Keras創(chuàng)建一個簡單的CNN模型,并用它對手寫數(shù)字進(jìn)行分類,需要的可以參考一下
    2023-05-05
  • 教你用Python腳本快速為iOS10生成圖標(biāo)和截屏

    教你用Python腳本快速為iOS10生成圖標(biāo)和截屏

    這篇文章主要介紹了教你用Python快速為iOS10生成圖標(biāo)和截屏的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Python二叉樹的鏡像轉(zhuǎn)換實(shí)現(xiàn)方法示例

    Python二叉樹的鏡像轉(zhuǎn)換實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Python二叉樹的鏡像轉(zhuǎn)換實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了二叉樹鏡像轉(zhuǎn)換的原理及Python相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • TensorFlow實(shí)現(xiàn)Softmax回歸模型

    TensorFlow實(shí)現(xiàn)Softmax回歸模型

    這篇文章主要介紹了TensorFlow實(shí)現(xiàn)Softmax回歸模型,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python標(biāo)準(zhǔn)庫os.path包、glob包使用實(shí)例

    Python標(biāo)準(zhǔn)庫os.path包、glob包使用實(shí)例

    這篇文章主要介紹了Python標(biāo)準(zhǔn)庫os.path包、glob包使用實(shí)例,本文直接給出代碼,代碼中有詳細(xì)注釋,需要的朋友可以參考下
    2014-11-11

最新評論