python 繪製直方圖,並且旋轉label標籤
ax.set_xticklabels(labels,rotation=120) # 旋轉標籤,避免標籤重疊覆蓋
label翻轉的實現,在輸出到頁面之前,使用:fig.autofmt_xdate() 或者 ax.set_xticklabels(group_labels, rotation=120) rotation就是翻轉的角度
# -*- coding: gbk -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
def draw_pie(labels,quants):
# make a square figure
plt.figure(1, figsize=(6,6))
# For China, make the piece explode a bit
expl = [0,0.1,0,0,0,0,0,0,0,0]
# Colors used. Recycle if not enough.
colors = ["blue","red","coral","green","yellow","orange"]
# Pie Plot
# autopct: format of "percent" string;
plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()
def draw_bar(labels,quants):
width = 0.4
ind = np.linspace(0.5,9.5,10)
# make a square figure
fig = plt.figure(1)
ax = fig.add_subplot(111)
# Bar Plot
ax.bar(ind-width/2,quants,width,color='green')
# Set the ticks on x-axis
ax.set_xticks(ind)
ax.set_xticklabels(labels,rotation=120) # 旋轉標籤,避免標籤重疊覆蓋
# labels
ax.set_xlabel('Country')
ax.set_ylabel('GDP (Billion US dollar)')
# title
ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.grid(True)
plt.show()
def draw_line(labels,quants):
ind = np.linspace(0,9,10)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(ind,quants)
ax.set_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
ax.set_xticklabels(labels)
plt.grid(True)
plt.show()
# quants: GDP
# labels: country name
labels = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy']
quants = [15094025.0, 11299967.0, 4457784.0, 4440376.0, 3099080.0, 2383402.0, 2293954.0, 2260803.0, 2217900.0,1846950.0]
draw_pie(labels,quants)
#draw_bar(labels,quants)
#draw_line(labels,quants)
DataFrame.plot( )函式
DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False,
sharex=None, sharey=False, layout=None,figsize=None,
use_index=True, title=None, grid=None, legend=True,
style=None, logx=False, logy=False, loglog=False,
xticks=None, yticks=None, xlim=None, ylim=None, rot=None,
xerr=None,secondary_y=False, sort_columns=False, **kwds)
引數詳解如下:
Parameters:
x : label or position, default None#指資料框列的標籤或位置引數
y : label or position, default None
kind : str
‘line’ : line plot (default)#折線圖
‘bar’ : vertical bar plot#條形圖
‘barh’ : horizontal bar plot#橫向條形圖
‘hist’ : histogram#柱狀圖
‘box’ : boxplot#箱線圖
‘kde’ : Kernel Density Estimation plot#Kernel 的密度估計圖,主要對柱狀圖新增Kernel 概率密度線
‘density’ : same as ‘kde’
‘area’ : area plot#不瞭解此圖
‘pie’ : pie plot#餅圖
‘scatter’ : scatter plot#散點圖 需要傳入columns方向的索引
‘hexbin’ : hexbin plot#不瞭解此圖
ax : matplotlib axes object, default None#**子圖(axes, 也可以理解成座標軸) 要在其上進行繪製的matplotlib subplot物件。如果沒有設定,則使用當前matplotlib subplot**其中,變數和函式通過改變figure和axes中的元素(例如:title,label,點和線等等)一起描述figure和axes,也就是在畫布上繪圖。
subplots : boolean, default False#判斷圖片中是否有子圖
Make separate subplots for each column
sharex : boolean, default True if ax is None else False#如果有子圖,子圖共x軸刻度,標籤
In case subplots=True, share x axis and set some x axis labels to invisible; defaults to True if ax is None otherwise False if an ax is passed in; Be aware, that passing in both an ax and sharex=True will alter all x axis labels for all axis in a figure!
sharey : boolean, default False#如果有子圖,子圖共y軸刻度,標籤
In case subplots=True, share y axis and set some y axis labels to invisible
layout : tuple (optional)#子圖的行列布局
(rows, columns) for the layout of subplots
figsize : a tuple (width, height) in inches#圖片尺寸大小
use_index : boolean, default True#預設用索引做x軸
Use index as ticks for x axis
title : string#圖片的標題用字串
Title to use for the plot
grid : boolean, default None (matlab style default)#圖片是否有網格
Axis grid lines
legend : False/True/’reverse’#子圖的圖例,新增一個subplot圖例(預設為True)
Place legend on axis subplots
style : list or dict#對每列折線圖設定線的型別
matplotlib line style per column
logx : boolean, default False#設定x軸刻度是否取對數
Use log scaling on x axis
logy : boolean, default False
Use log scaling on y axis
loglog : boolean, default False#同時設定x,y軸刻度是否取對數
Use log scaling on both x and y axes
xticks : sequence#設定x軸刻度值,序列形式(比如列表)
Values to use for the xticks
yticks : sequence#設定y軸刻度,序列形式(比如列表)
Values to use for the yticks
xlim : 2-tuple/list#設定座標軸的範圍,列表或元組形式
ylim : 2-tuple/list
rot : int, default None#設定軸標籤(軸刻度)的顯示旋轉度數
Rotation for ticks (xticks for vertical, yticks for horizontal plots)
fontsize : int, default None#設定軸刻度的字型大小
Font size for xticks and yticks
colormap : str or matplotlib colormap object, default None#設定圖的區域顏色
Colormap to select colors from. If string, load colormap with that name from matplotlib.
colorbar : boolean, optional #圖片柱子
If True, plot colorbar (only relevant for ‘scatter’ and ‘hexbin’ plots)
position : float
Specify relative alignments for bar plot layout. From 0 (left/bottom-end) to 1 (right/top-end). Default is 0.5 (center)
layout : tuple (optional) #佈局
(rows, columns) for the layout of the plot
table : boolean, Series or DataFrame, default False #如果為正,則選擇DataFrame型別的資料並且轉換匹配matplotlib的佈局。
If True, draw a table using the data in the DataFrame and the data will be transposed to meet matplotlib’s default layout. If a Series or DataFrame is passed, use passed data to draw a table.
yerr : DataFrame, Series, array-like, dict and str
See Plotting with Error Bars for detail.
xerr : same types as yerr.
stacked : boolean, default False in line and
bar plots, and True in area plot. If True, create stacked plot.
sort_columns : boolean, default False # 以字母表順序繪製各列,預設使用前列順序
secondary_y : boolean or sequence, default False ##設定第二個y軸(右y軸)
Whether to plot on the secondary y-axis If a list/tuple, which columns to plot on secondary y-axis
mark_right : boolean, default True
When using a secondary_y axis, automatically mark the column labels with “(right)” in the legend
kwds : keywords
Options to pass to matplotlib plotting method
Returns:axes : matplotlib.AxesSubplot or np.array of them
原文:https://blog.csdn.net/brucewong0516/article/details/80524442
原文:https://blog.csdn.net/HUSTLX/article/details/50850092