溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python中怎么使用matplotlib實現數據可視化

發布時間:2021-07-10 13:55:51 來源:億速云 閱讀:190 作者:Leah 欄目:大數據

Python中怎么使用matplotlib實現數據可視化,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

數據可視化設計

本期我們構建一組簡單的時間變化圖表數據,當然還有我們常用的顏色字典構建。具體如下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

test_dict = {'x':[0,5,10,15,20,25,30],'year':['1990','1995','2000','2005','2010','2015','2020']}
artist_04 = pd.DataFrame(test_dict)

color = ("#F5A34D", "#F26F77", "#48AEBA", "#A3BA74","#958298", "#B88357",'#608CB1' )
data = artist_04['x'].to_list()
data_color = dict(zip(data,color))
data_color

顏色字典如下:

{0: '#F5A34D',
 5: '#F26F77',
 10: '#48AEBA',
 15: '#A3BA74',
 20: '#958298',
 25: '#B88357',
 30: '#608CB1'}

詳細繪圖代碼如下:

fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='#FFF7F2',edgecolor='#FFF7F2')
ax.set_facecolor('#FFF7F2')
#繪制中間橫線
ax.set_ylim(-.5,1.5)
#繪制具有端點形狀的直線
ax.plot([-3,38],[.5,.5],"-o",lw=1.2,color='gray',markerfacecolor="w",mec='gray',ms=5,
        markeredgewidth=1.,zorder=1)

#分上下情況繪制點、線混合圖形
for x in [0,10,20,30]:
    #繪制橫線上的散點,顏色不同
    ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)
    #繪制疊加在顏色散點之上的散點,顏色為白色
    ax.scatter(x,.5,s=50,zorder=3,color='white')
    #繪制散點和圓柱之間的連接線,端點為圓點
    ax.plot([x,x],[.5,.5+.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)
    #繪制橫置圓柱圖
    ax.plot([x,x+7.5],[.5+.6,.5+.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)
    ax.scatter(x,.5+.6,s=80,zorder=3,color='white')
    ax.text(x+4,.5+.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center', 
            va='center')
    #添加年份
    ax.text(x-1.4,.5+.2,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,
            fontweight='bold',rotation=90)
    
    #添加描述文字
    ax.text(x+.5,.5+.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',
            ha='left', va='center',fontsize = 4,color='gray')

for x in [5,15,25]:
    #繪制橫線上的散點,顏色不同
    ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)
    #繪制疊加在顏色散點之上的散點,顏色為白色
    ax.scatter(x,.5,s=50,zorder=3,color='white')
    #繪制散點和圓柱之間的連接線,端點為圓點
    ax.plot([x,x],[.5,.5-.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)
    #繪制橫置圓柱圖
    ax.plot([x,x+7.5],[.5-.6,.5-.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)
    ax.scatter(x,.5-.6,s=80,zorder=3,color='white')
    ax.text(x+4,.5-.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center', 
            va='center')
    #添加描述文字
    ax.text(x+.5,.5-.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',
            ha='left', va='center',fontsize = 4,color='gray')
    #添加年份
    ax.text(x-1.4,.5-.4,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,
            fontweight='bold',rotation=90)
    
#添加題目文本
ax.axis('off')
ax.text(.49,1.15,'\nTIMELINE INFOGRAPHICS',transform = ax.transAxes,
        ha='center', va='center',fontsize = 20,color='gray',fontweight='light')
ax.text(.92,.00,'\nVisualization by DataCharm',transform = ax.transAxes,
        ha='center', va='center',fontsize = 5,color='black')
plt.savefig(r'F:\DataCharm\商業藝術圖表仿制\artist_04.png',width=8,height=4,
            dpi=900,bbox_inches='tight',facecolor='#FFF7F2')

知識點:

(1)熟悉ax.plot()函數方法,其他參數設置不同對結果也不同。

(2)ax.scatter()繪制散點。

(3)ax.text()文本的靈活添加。

(4)顏色的合理選擇。

結果圖表如下:

Python中怎么使用matplotlib實現數據可視化

看完上述內容,你們掌握Python中怎么使用matplotlib實現數據可視化的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女