matplotlib

The Post Created(Updated) On 04/23/2022,Please note the timeliness of the article!

Data

Github/YOUTUBE_studying

draw

import pandas as pd
unrate = pd.read_csv('DATA.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE']) # date format conversion
print(unrate.head(12))

# drawing equipment
import matplotlib.pyplot as plt
#%matplotlib inline
#Using the diffenent pyplot functions, we can create, customize, and display a plot . For example, we can use
# plt.plot() # drawing
# plt.show() # show

first_twelve = unrate[0:12] #value

## Parameter Description
print('\n\n----plot(x,y,c)----------')
plt.plot(first_twelve['DATE'],first_twelve['VALUE'],c='red',label='test') #Corresponding data for the x and y axes respectively, c:color,label
plt.legend(loc='best') # Display label, loc is the display position (best is the best position considered by the system)
plt.show()


# print(help(plt.plot))

## x,y axis font angle change / prevent fonts from being too crowded together
print('\n\n------------x,y axis font angle change/prevent fonts from being too crowded together------')
plt.plot(first_twelve['DATE'],first_twelve['VALUE'])
plt.xticks(rotation=45) # Transform the x-coordinate scale by 45 degrees
plt.yticks(rotation=180)
plt.show()

## Add x,y axis description
print('\n---------Add x,y axis description------------')
plt.plot(first_twelve['DATE'],first_twelve['VALUE'])
plt.xticks(rotation=0) # Transform the x-coordinate scale by 45 degrees
plt.yticks(rotation=0)
plt.xlabel('MONTH') # x coordinate description
plt.ylabel('VALUE')
plt.title('Monthly Unemployment Trends 1948') #icon title
plt.show()

draw subplots

## draw subplots
import matplotlib.pyplot as plt
fig = plt.figure() # Specify a default drawing interval
# add_subplot(first,second,index) first means number of Row,second means number of Cloumn,index means which place you want to show
ax1 = fig.add_subplot(4,3,1)
ax2 = fig.add_subplot(4,3,3)
ax3 = fig.add_subplot(4,3,12)

plt.show()

Specify the size of the drawing area

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
# fig = plt.figure(figsize=(3,3)) #Specify the size of the drawing area; (length, width)
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.plot(np.random.randint(1,5,5),np.arange(5)) # randint
ax2.plot(np.arange(10)*2,np.arange(10))

plt.show()

# print(help(np.random.randint)) # random number
# print(help(np.arange))
# print(np.arange(10))

draw two lines in one graph

## draw two lines in one graph
import matplotlib.pyplot as plt
import pandas as pd
Data = pd.read_csv('DATA.csv')
Data['DATE'] = pd.to_datetime(Data['DATE'])
print(Data.head(12))

print('-----------------\n\n')

Data['MONTH'] = Data['DATE'].dt.month
# Data['MONTH'] = Data['DATE'].dt.month

plt.xlabel('MONTH')
plt.ylabel('VALUE')
plt.title('The Unployment In 1948')


plt.plot(Data[0:6]['MONTH'],Data[0:6]['VALUE'],c='red',label='0-6') # (x, y, Line_color, label)
plt.plot(Data[6:12]['MONTH'],Data[6:12]['VALUE'],c='blue',label='6-12')
plt.legend(loc='best')

plt.show()

Add label description to the line in the figure

## Add label description to the line in the figure
import matplotlib.pyplot as plt
import pandas as pd
Data = pd.read_csv('DATA.csv')
Data['DATE'] = pd.to_datetime(Data['DATE'])

fig = plt.figure(figsize=(10,6))

colors = ['red','blue','green','orange','black']
for i in range(5):
    start_index = i*12
    end_index = (i+1)*12
    subset = Data[start_index:end_index]
    label = str(1948 + i)
    plt.plot(subset['DATE'],subset['VALUE'],c=colors[i],label=label)

plt.legend(loc='best') # Display the label

plt.xlabel('Data')
plt.ylabel('VALUE')
plt.title('Monthly Unemployment Trends,1948-1952')
#print(plt.legend)
plt.show()

Column chart

## Column chart
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

print('\n\n---------\n\n')

# bar_heights = norm_datas.ix[0,cols].values ​​# old using
bar_heights = norm_datas.loc[0,cols].values
print(bar_heights)

bar_postions = arange(6) + 1 # column spacing
print(bar_postions) # Position, corresponding to A, B, C, D, E, F in turn

print('\n\n-----vertical chart----\n\n')

# fig = plt.figure()
fig, ax = plt.subplots()

# print(help(plt.subplots))
ax.bar(bar_postions,bar_heights,0.5) #Note ax.bar(x,y,n) where x = y, otherwise an error will be reported; n refers to the width of the column

ax.set_xticks(bar_postions) #If you log out, it will be displayed from the B value, which should be used in conjunction with the following statement;
ax.set_xticklabels(norm_datas,rotation = -45)
# The first parameter specifies what the coordinates are, which can turn 123 into the original clom value;
# The second specifies the inclination of the x-axis

# ax.set_yticks(bar_heights)
# ax.set_yticklabels(norm_datas,rotation = 45)

ax.set_xlabel('Media ') # x-axis
ax.set_ylabel('Rating') # y axis
ax.set_title('Average User Rating For 0') # Title

plt.show()



print('\n\n\n----------Horizontal Chart---------\n\n')
fig, ax = plt.subplots() #ax generally refers to the actual axis; fig usually sets the parameters of the graph (the appearance of the control graph)

# print(help(plt.subplots))
ax.barh(bar_postions,bar_heights,0.5) #Note ax.bar(x,y,n) where x = y, otherwise an error will be reported; n refers to the width of the column

ax.set_yticks(bar_postions) #If you log out, it will be displayed from the B value, which should be used in conjunction with the following statement;
ax.set_yticklabels(norm_datas,rotation = 0)
# The first parameter specifies what the coordinates are, which can turn 123 into the original clom value;
# The second specifies the inclination of the x-axis

# ax.set_yticks(bar_heights)
# ax.set_yticklabels(norm_datas,rotation = 45)

ax.set_ylabel('Media ') # x-axis
ax.set_xlabel('Rating') # y axis
ax.set_title('Average User Rating For 0') # Title

plt.show()

##scatterplot

I don’t quite understand the way of data in scatter plots

## scatter plot

###? ? ? ? ?
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

fig,ax = plt.subplots()
ax.scatter(norm_datas['A'],norm_datas['B']) # View the ratings of the two media
ax.set_xlabel('A')
ax.set_yla翻译类型
文本翻译
原文
eu.org域名申请
Bel('B') plt.Show() # print(help(ax.Scatter)) # Switching Axes fig = plt.Figure(figsize=(5,10)) # zǐ tú ax1 = fig.Add_subplot(2,1,1) ax2 = fig.Add_subplot(2,1,2) ax1.Scatter(norm_datas['A'],norm_datas['B']) ax1.Set_xlabel('A') ax1.Set_ylabel('B') ax2.Scatter(norm_datas['A'],norm_datas['B']) ax2.Set_xlabel('A') ax2.Set_ylabel('B') plt.Show() ``` ## àn qūjiān lái tǒngjì shùliàng ```python ## tú # àn qūjiān lái tǒngjì shùliàng import pandas as pd import matplotlib.Pyplot as plt from numpy import arange Mov = pd.Read_csv('data1.Csv') cols = ['A','B','C','D','E','F'] norm_datas = Mov[cols] print(norm_datas[:8]) Fig,ax = plt.Subplots() ax.Hist(norm_datas['C']) # bù zhǐdìng zé mòrèn zìdòng shēngchéng bins # ax.Hist(norm_datas['C'],bins=10) # bins jí kě róngnà de tiáo xíng shùliàng # bins: Integer or array_like, optional # zhège cānshù zhǐdìng bin(xiāngzi) de gè shù, yě jiùshì zǒnggòng yǒu jǐ tiáo tiáo zhuàng tú # bins cúnzài yīgè zuìxiǎo zhí # ax.Hist(norm_datas['C'],range=(5,7),bins=20)# range(x,y) zhǐdìng qǐ shǐ hé jiéshù qūjiān # print(help(ax.Hist)) ### ### bins wèi lǐjiě ### plt.Show() ``` ## zǐ tú xiǎnshì ```python ## tú # zǐ tú xiǎnshì import pandas as pd import matplotlib.Pyplot as plt from numpy import arange Mov = pd.Read_csv('data1.Csv') cols = ['A','B','C','D','E','F'] norm_datas = Mov[cols] print(norm_datas[:8]) Fig = plt.Figure(figsize=(5,20)) ax1 = fig.Add_subplot(4,1,1) ax2 = fig.Add_subplot(4,1,2) ax3 = fig.Add_subplot(4,1,3) ax4 = fig.Add_subplot(4,1,4) ax1.Hist(norm_datas['A'],range=(0,5)) ax1.Set_title('A') ax1.Set_ylim(0,5) # zhǐdìng y zhóu qūjiān ax2.Hist(norm_datas['B'],range=(0,5)) ax2.Set_title('B') ax2.Set_ylim(0,5) ax3.Hist(norm_datas['C'],range=(0,5)) ax3.Set_title('C') ax3.Set_ylim(0,5) ax4.Hist(norm_datas['D'],range=(0,5)) ax4.Set_title('D') ax4.Set_ylim(0,5) plt.Show() ``` ## hé tú ```python ## hé tú # bǎ shùjù fēnchéng 4 fēn,4 fēn tú import pandas as pd import matplotlib.Pyplot as plt from numpy import arange Mov = pd.Read_csv('data1.Csv') cols = ['A','B','C','D','E','F'] norm_datas = Mov[cols] print(norm_datas[:8]) Fig,ax = plt.Subplots() ax.Boxplot(norm_datas['A']) ax.Set_xticklabels(['A']) ax.Set_ylim(0,8) plt.Show() ``` ### hé tú duō gè ```python ### hé tú duō gè import pandas as pd import matplotlib.Pyplot as plt from numpy import arange Mov = pd.Read_csv('data1.Csv') cols = ['A','B','C','D','E','F'] norm_datas = Mov[cols] print(norm_datas[:8]) Fig,ax = plt.Subplots() ax.Boxplot(norm_datas[cols].Values) ax.Set_xticklabels(cols,rotation=0) ax.Set_ylim(0,17) plt.Show() ```
收起
2,305 / 5,000
翻译结果
bel('B')
plt.show()

# print(help(ax.scatter))

# Switching Axes
fig = plt.figure(figsize=(5,10))

# subgraph
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.scatter(norm_datas['A'],norm_datas['B'])
ax1.set_xlabel('A')
ax1.set_ylabel('B')

ax2.scatter(norm_datas['A'],norm_datas['B'])
ax2.set_xlabel('A')
ax2.set_ylabel('B')

plt.show()

Count numbers by interval

## picture
# Count numbers by interval
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

fig,ax = plt.subplots()

ax.hist(norm_datas['C']) # If not specified, bins will be automatically generated by default
# ax.hist(norm_datas['C'],bins=10) # The number of bars that bins can hold
# bins : integer or array_like, optional
# This parameter specifies the number of bins (boxes), that is, there are a total of several bar graphs
# bins has a minimum
# ax.hist(norm_datas['C'],range=(5,7),bins=20) # range(x,y) specifies the start and end range

# print(help(ax.hist))


###
### bins not understood
###

plt.show()

subgraph display

## picture
# subgraph display
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

fig = plt.figure(figsize=(5,20))
ax1 = fig.add_subplot(4,1,1)
ax2 = fig.add_subplot(4,1,2)
ax3 = fig.add_subplot(4,1,3)
ax4 = fig.add_subplot(4,1,4)

ax1.hist(norm_datas['A'],range=(0,5))
ax1.set_title('A')
ax1.set_ylim(0,5) # Specify the y-axis interval

ax2.hist(norm_datas['B'],range=(0,5))
ax2.set_title('B')
ax2.set_ylim(0,5)

ax3.hist(norm_datas['C'],range=(0,5))
ax3.set_title('C')
ax3.set_ylim(0,5)


ax4.hist(norm_datas['D'],range=(0,5))
ax4.set_title('D')
ax4.set_ylim(0,5)

plt.show()


Box plot

## Box plot
# Divide the data into 4 points, 4 points map
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

fig,ax = plt.subplots()

ax.boxplot(norm_datas['A'])
ax.set_xticklabels(['A'])
ax.set_ylim(0,8)

plt.show()

Multiple box plots

### Multiple box plots
import pandas as pd
import matplotlib.pyplot as plt
from numpy import arange

Mov = pd.read_csv('data1.csv')
cols = ['A','B','C','D','E','F']
norm_datas = Mov[cols]
print(norm_datas[:8])

fig,ax = plt.subplots()
ax.boxplot(norm_datas[cols].values)
ax.set_xticklabels(cols,rotation=0)
ax.set_ylim(0,17)

plt.show()

Copyright

Unless otherwise noted, all work on this blog is licensed under a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) License. Reprinted with permission from -
https://blog.emperinter.info/2022/04/23/matplotlib


CouPon

alibaba cloud20 dollars
Vultr10 Dollars
Bandwagon HostMaybe some Coupon?
Domain | namesiloemperinter 1Dollars