matplotlib画图+sklearn数据集+Pandas绘图
Matplotlib
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 [1] 。
通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等
画图时折线图时可以用其他符号代替直线,见http://www.runoob.com/numpy/numpy-matplotlib.html
关于matplotlib写的比较好的博客
https://blog.csdn.net/wizardforcel/article/details/54407212
https://blog.csdn.net/qq_34859482/article/details/80617391
Sklearn数据集
官网:https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html
参考博客:https://blog.csdn.net/weixin_42039090/article/details/80614918
画子图
1、方法一
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,5)
y1=np.sin(np.pi*x)
y2=np.sin(np.pi*2*x)
plt.subplot(211)
plt.plot(x,y1)
plt.subplot(212)
plt.plot(x,y2)
plt.show()

2、方法二
import numpy as np
import matplotlib.pyplot as plt
fig,ax=plt.subplots(2,2)
ax[0][0].plot(x,y1)
ax[0][1].plot(x,y2)
ax[1][0].plot(x,y1)
ax[1][1].plot(x,y2)
plt.show()

利用pandas绘图
pandas文档:http://pandas.pydata.org/pandas-docs/stable/visualization.html
seaborn
Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。
Seaborn API:http://seaborn.pydata.org/api.html#categorical-api
import seaborn as sns
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
import matplotlib.pyplot as plt
df = sns.load_dataset('flights')
# 透视表
df = df.pivot(index='month',columns='year',values='passengers')
sns.heatmap(df) #图一
df.plot() # 图二
sns.heatmap(df,annot=True,fmt='d') #图三
s = df.sum()
sns.barplot(s.index,s.values)#图四
s.plot(kind='bar') #图五





Pandas_datareader
pandas_datareader是一个远程获取金融数据的Python工具,通过它可以方便获得下面公司和机构的数据:
Yahoo! Finance//雅虎金融
Google Finance//谷歌金融
Enigma//Enigma是一个公共数据搜索的提供商
St.Louis FED (FRED)//圣路易斯联邦储备银行
Kenneth French’s data library//肯尼斯弗兰奇资料库
World Bank//世界银行
OECD//经合组织
Eurostat//欧盟统计局
Thrift Savings Plan//美国联邦政府管理离退休的组织
Oanda currency historical rate //外汇经纪商
Nasdaq Trader symbol definitions //纳斯达克
pandas-datareader 0.7.0:https://pypi.org/project/pandas-datareader/
import pandas_datareader as pdr
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
import matplotlib.pyplot as plt
from datetime import datetime
import seaborn as sns
start = datetime(2015,9,20)
alibaba = pdr.get_data_yahoo('BABA',start=start)
amazon = pdr.get_data_yahoo('AMZN',start=start)
amazon.to_csv('AMZN.csv')
alibaba['Adj Close'].plot(legend=True)#图一
alibaba['Volume'].plot(legend=True)#图二
alibaba['Adj Close'].plot()
amazon['Adj Close'].plot()
plt.show()#图三
alibaba['high-low'] = alibaba['High']-alibaba['Low'] # 图四
alibaba['high-low'].plot()
plt.show() #图五
alibaba['daily-return'] = alibaba['Adj Close'].pct_change()
alibaba['daily-return'].plot(figsize=(10,4),linestyle='--',marker='o')
plt.show()#图六
alibaba['daily-return'].plot(kind='hist')
plt.show()#图七
sns.distplot(alibaba['daily-return'].dropna(),bins=100,color='purple')#图八






