Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use matplotlib to realize data Visualization in Python

Shulou Source: shulou.com Published: 2022-06-02 07:15:15 10月02日 Update

Python how to use matplotlib to achieve data visualization, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Data visualization design

In this issue, we build a simple set of time change chart data and, of course, our commonly used color dictionary construction. The details are as follows:

Import pandas as pdimport numpy as npimport matplotlib.pyplot as plttest_dict = {'Xerox: [0petre 5pyrus 10pyrus 15pyrus 20pyrus 25pyrus 30],' year': ['1990pyrrhenium] 2005pyrrh] 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

The color dictionary is as follows:

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

The detailed drawing code is as follows:

Fig,ax = plt.subplots (figsize= (8jue 4), dpi=200,facecolor='#FFF7F2',edgecolor='#FFF7F2') ax.set_facecolor ('# FFF7F2') # draw the middle horizontal line ax.set_ylim (- .5) # draw a straight line ax.plot with the shape of the end ([- 3cm38], [.5mem.5], "- o", lw=1.2,color='gray',markerfacecolor= "w", mec='gray',ms=5, markeredgewidth=1. Zorder=1) # draw a mixed pattern of points and lines up and down. For x in: # draw scattered dots on horizontal lines. Different colors ax.scatter (zorder=2) # draw the scattered dots superimposed on the color scatters, and the color is white ax.scatter (xmem.5pm swatches 5pm 5pm 3cinematic colors whiteite) # draw the connection line between the scatter and the cylinder The endpoints are dots ax.plot ([xdome x], [.5Power.6], "- o", color=data_ color [x], lw=.6,mfc= "w", ms=5,mew=1.2,zorder=3) # draw a horizontal cylinder ax.plot ([xjournal x = 7.5], [.5 + .6, .5 + .6], lw=15,color=data_ [color x], solid_capstyle='round',zorder=1) ax.scatter (x camera .5pm. 6pr. Color='white') ax.text (xmai 4Pol. 5pm. 6Personality Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center', va='center') # add year ax.text (Xmure 1.4rect .5mm .2memoir artist04.loc.values [04 ['x'] = = x yearbook] .values [0], color='#686866',fontsize=12, fontweight='bold' Rotation=90) # add the description text ax.text (xcam.5mem.5ped.3Power.3pageoptionally, the text can bedisplayed\ nin anotherpositionxytext.Anarrow\ npointingfrom the text totheannotated\ npoint xy canthen beaddedbydefining\ narrowprops.', ha='left', va='center',fontsize = 4cinematic colors, colors Different colors ax.scatter (zorder=2) # draw the scattered dots superimposed on the color scatters, and the color is white ax.scatter (xmem.5pm swatches 5pm 5pm 3cinematic colors whiteite) # draw the connection line between the scatter and the cylinder The endpoints are dots ax.plot ([xmemx], [.5meme.5mur.6], "- o", color=data_ color [x], lw=.6,mfc= "w", ms=5,mew=1.2,zorder=3) # draw a horizontal cylinder ax.plot ([xmemxen7.5], [.5-.6, .5-.6], lw=15,color=data_ [colorx], solid_capstyle='round',zorder=1) ax.scatter (x camera .5w.6) Color='white') ax.text (xqun.5fu.6Morem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center', va='center') # add the description text ax.text (xcuri.5meme .5mer.3authoritative optionally, the text can bedisplayed\ nin anotherpositionxytext.Anarrow\ npointingfrom the text totheannotated\ npoint xy canthen beaddedbydefining\ narrowprops.', ha='left', va='center',fontsize = 4 Color='gray') # add year ax.text (xmur1.4meme .5mae .4recordsartist04.loc.values [0], color='#686866',fontsize=12, fontweight='bold',rotation=90) # add title text ax.axis ('off') ax.text (.49jue 1.15jue\ nTIMELINE INFOGRAPHICS',transform = ax.transAxes, ha='center', va='center') Fontsize = 20 plt.savefig colorweights graylights) ax.text (.92 dpi=900,bbox_inches='tight',facecolor='#FFF7F2' .00'\ nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 5 colors colors blackout') plt.savefig (ringing F:\ DataCharm\ commercial art chart imitation\ artist_04.png',width=8,height=4, dpi=900,bbox_inches='tight',facecolor='#FFF7F2')

Knowledge points:

(1) be familiar with the method of ax.plot () function, and the results are different with different settings of other parameters.

(2) ax.scatter () draws scatters.

(3) flexible addition of ax.text () text.

(4) reasonable choice of color.

The chart of the result is as follows:

After reading the above, have you mastered how to use matplotlib to achieve data visualization in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Color data different cylinder visualization chart method horizontal line end point between content dot dictionary year text text more white result connection line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Shulou Information Microsoft MariaDB