table of Contents
1, a
2, drawing techniques Series object
3, object drawing techniques DataFrame
1. Description
Object Series and DataFrame type also supports graphics rendering, you can use the object's plot method. If the drawing data exists Series or DataFrame object, we can draw directly, without using plt.plot.
Drawing Series directly plot method and DataFrame objects, sometimes seems to be particularly convenient, especially to draw some simple graphics, it is very nice!
Note: When using the plot depicts a graphical method Series and DataFrame object, still You can use various drawing parameters matplotlib in to beautify your own graphics.
2, the object of drawing techniques Series
1) graphics rendering common
The default plot is a line graph plotted, but we can adjust the value of its parameter kind, to draw various other types of graphics.
① common type of FIG.
- line: Line Chart
- bar: bar graph
- barh: bar
- hist: Histogram
- kde / density: FIG kernel density
- pie: pie
- box: Boxplot
- area: an area chart
② common parameters
- color
- alpha
- stacked: whether the stack.
③ two kinds of syntax
- Formats: s.plot (kind = "FIG type")
- For example as follows: s.plot (kind = "line")
- Two formats: s.plot FIG type ().
- For example as follows: s.plot.line ()
As follows:
s1 = pd.Series([1,3,8,10,12])
s2 = pd.Series([5,2,6,4,8])
s1.plot(kind="line",c="r")
s2.plot.line(c="b")
# 可以看到,我们仍然可以调用matplotlib的其他绘图参数,完善自己的图形。
plt.legend(["2016","2017"],loc="best")
The results are as follows:
2) Case Operation
① draw a line chart
s1 = pd.Series([5,2,6,4,8])
s1.plot(kind="line",c="r")
The results are as follows:
② Draw a bar graph (also known as a bar graph)
s1 = pd.Series([5,2,6,4,8])
s1.plot.bar(color="b")
# 当没有这句代码时,横坐标是“睡着”的,因此调整一下横坐标标签的方向。
plt.xticks(rotation=360)
The results are as follows:
③ drawing a horizontal bar graph (bar)
s1 = pd.Series([5,2,6,4,8])
s1.plot.barh(color="g")
The results are as follows:
④ draw a histogram
s1 = pd.Series([5,2,6,4,8,5,2,7,4,6,1,8,4,6,3,8,2,6,4,8,5,2,7])
s1.plot.hist(color="m")
The results are as follows:
⑤ kernel density plotted in FIG.
s1 = pd.Series([5,2,6,4,8,5,2,7,4,6,1,8,4,6,3,8,2,6,4,8,5,2,7])
s1.plot(kind="kde",color="r")
The results are as follows:
⑥ draw a pie chart
s1 = pd.Series([1,1,2,2,3])
# 求出Series中每个元素的占比
s1 = s1.value_counts()/s1.shape
display(s1)
s1.plot(kind="pie")
plt.axis("equal")
The results are as follows:
⑦ drawing boxplot
s1 = pd.Series([5,2,6,4,8,5,2,7,4,6,1,8,4,6,3,8,2,4,8,5,2,7])
s1.plot(kind="box",color="r")
The results are as follows:
⑧ drawing area chart
s1 = pd.Series([1, 3, 8, 10, 12])
s1.plot(kind="area",color="orangered",alpha=0.3)
plt.grid()
The results are as follows:
3, the object of drawing skills DataFrame
① draw a line chart
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="柱形图1")
df1.plot(kind="line")
plt.ylim(0,10)
plt.xticks(np.arange(0,5),["果汁","矿泉水","绿茶","其它","碳酸饮料"])
The results are as follows:
② draw bar
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="柱形图1")
df1.plot(kind="bar",stacked=True)
plt.ylim(0,17)
plt.xticks(np.arange(0,5),["果汁","矿泉水","绿茶","其它","碳酸饮料"])
for x,y in enumerate(df["男"]):
plt.text(x,y/2-0.5,y,ha="center",va="bottom",fontsize=15)
for x,y in enumerate(df["女"]):
plt.text(x,y/2+df["男"][x]-0.5,y,ha="center",va="bottom",fontsize=15)
for xy1 in enumerate(df["男"]+df["女"]):
plt.annotate("{}".format(xy1[1]),xy=xy1,ha="center",va="bottom")
plt.tight_layout()
plt.savefig("不同饮料类型的男、女人数的堆积条形图",dpi=300)
The results are as follows:
③ drawing a horizontal stacked bar chart
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="柱形图1")
df.plot(kind="barh", stacked=True,color=['lightcoral','lightslategrey'])
for x,y in enumerate(df["男"]):
plt.text(y/2,x,y,ha="center",va="center",fontsize=15)
for x,y in enumerate(df["女"]):
plt.text(y/2+df["男"][x],x,y,ha="center",va="center",fontsize=15)
for x,y in enumerate(df["男"]+df["女"]):
plt.text(y+0.3,x,y,ha="center",va="center",fontsize=15)
plt.yticks(np.arange(0,5),["果汁","矿泉水","绿茶","其它","碳酸饮料"])
plt.tight_layout()
plt.savefig("不同饮料类型的男、女人数的水平堆积条形图",dpi=300)
The results are as follows:
④ kernel density plotted in FIG.
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="柱形图1")
df1 = df[["男","女"]]
df1.plot(kind="kde",color=['lightcoral','lightslategrey'],lw=3)
plt.tight_layout()
plt.savefig("不同饮料类型的男、女人数的核密度图",dpi=300)
The results are as follows:
⑤ drawing boxplot
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="箱线图")
df1 = df.iloc[:,1:]
f = df1.plot(kind="box",showfliers = True,
color = dict(boxes='DarkGreen',whiskers='DarkOrange',medians='DarkBlue',caps='Gray'))
plt.xticks(rotation=70,fontproperties = 'simhei')
plt.tight_layout()
plt.savefig("8门课程考试成绩的箱线图",dpi=300)
The results are as follows:
detailed parameters, see the article: https://blog.csdn.net/weixin_30935137/article/details/80685957
⑥ drawing area chart
df = pd.read_excel(r"C:\Users\黄伟\Desktop\matplotlib.xlsx",
sheet_name="面积图")
display(df.T)
df = df.T
df.plot(kind="area",figsize=(6,5))
plt.xticks(np.arange(3),["2018年","2019年","2020年"])
plt.yticks(np.arange(0,15001,5000))
The results are as follows:
on the interpretation of FIG Area: Area is formed on the basis of the line graph above the region between the fold line and the line graph will coordinate axes, using a color fill, the fill is what we call area, fill color to better highlight trends information. And line charts like area charts emphasize the amount of time the extent of change over time.