图表的基本元素及样式参数
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
图名、图例、轴标签、轴边界、轴刻度、轴刻度标签等
df=pd.DataFrame(np.random.rand(10,2), columns=['A', 'B'])
df.head()
fig=df.plot(figsize=(6,4))
plt.title("Interrsting Graph")
plt.xlabel("Plot Number")
plt.ylabel("Important var")
plt.legend(loc=0)
plt.xlim([0,12])
plt.ylim([0,1.2])
plt.xticks(range(12))
plt.yticks([0.0, 0.3, 0.6, 0.9, 1.2])
fig.set_xticklabels("%.1f" %i for i in range(12))
fig.set_yticklabels("%.2f" %i for i in [0.0, 0.3, 0.6, 0.9, 1.2])
网格、刻度属性、坐标轴显示
x=np.linspace(-np.pi, np.pi, 256, endpoint=True)
c,s = np.cos(x), np.sin(x)
plt.plot(x, c)
plt.plot(x, s)
plt.grid(True, linestyle='--', color='gray', linewidth='0.5', axis='both')
plt.tick_params(bottom=True, top=False, left=True, right=True)
plt.tick_params(color='red', labelcolor='blue', length=10, width=3,labelright=True, labelsize=10,pad=10)
import matplotlib
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'inout'
linestyle参数设置
plt.plot([i**2 for i in range(100)],
linestyle=':'
)
marker参数:线上的点的样式
s=pd.Series(np.random.randn(100).cumsum())
s.plot(linestyle='-.', marker='<')
color参数
plt.hist(np.random.randn(100),
color = 'r',alpha = 0.8)
style参数,可以包含linestyle,marker,color
ts = pd.Series(np.random.randn(1000).cumsum(), index=pd.date_range('1/1/2000', periods=1000))
ts.plot(style = '--g.',grid = True)
刻度
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
t = np.arange(0.0, 100.0, 1)
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)
ax = plt.subplot(111)
plt.plot(t,s,'--*')
plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'both')
xmajorLocator = MultipleLocator(10)
xmajorFormatter = FormatStrFormatter('%.0f')
xminorLocator = MultipleLocator(5)
ymajorLocator = MultipleLocator(0.5)
ymajorFormatter = FormatStrFormatter('%.1f')
yminorLocator = MultipleLocator(0.1)
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_major_locator(ymajorLocator)
ax.yaxis.set_major_formatter(ymajorFormatter)
ax.yaxis.set_minor_locator(yminorLocator)
ax.xaxis.grid(True, which='both')
ax.yaxis.grid(True, which='minor')
注解
df = pd.DataFrame(np.random.randn(10,2))
df.plot(style = '--o')
plt.text(5,0.5,'hahaha',fontsize=10)
图表输出
df = pd.DataFrame(np.random.randn(1000, 4), columns=list('ABCD'))
df = df.cumsum()
df.plot(style = '--.',alpha = 0.5)
plt.legend(loc = 'upper left')
plt.savefig('E:/test/pic.png',
dpi=400,
bbox_inches = 'tight',
facecolor = 'g',
edgecolor = 'b')