import numpy as np
import matplotlib.pyplot as plt
# 用linspace()函数指定横坐标,同时规定起点和终点分别为0和2
x=np.linspace(0,20)
# 实线
plt.plot(x,.5+x)
# 虚线
plt.plot(x,1+2*x,'--')
# 展示
plt.show()
对数图分为半对数图和双对数图。
半对数图:一个坐标轴采用线性标度,另一个坐标轴使用对数刻度,对应的函数是semilogx()函数和semilogy()函数
双对数图: 两个坐标轴都采用对数刻度,对应的函数是matplotlib.pyplot.loglog()函数
我随便编辑了一个CSV文档来举例
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv(r'Desktop\test.csv')
years=df.index.values
counts=df['counts'].values
# polyfit函数是用于进行曲线拟合的一个函数
poly=np.polyfit(years,np.log(counts),deg=1)
print("poly",poly)
# 绘制图像
plt.semilogy(years,counts,'o')
plt.semilogy(years,np.exp(np.polyval(poly,years)))
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 一条折线
s=pd.Series(np.random.randn(10),index=np.arange(0,100,10))
s.plot()
plt.show()
# 多条折线
df=pd.DataFrame(np.random.randn(10,3).cumsum(0),columns=['A','B','C'],index=np.arange(0,100,10))
df.plot()
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 一组数据
data=pd.Series(np.random.rand(6),index=list('abcdef'))
# kind参数为bar时代表垂直柱状图
data.plot(kind='bar',color='r',alpha=1)
plt.show()
# kind参数为barh时代表水平柱状图
data.plot(kind='barh',color='r',alpha=1)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.Series(np.random.randn(6), index=list('abcdef'))
data.plot(kind='bar', color='r', alpha=1)
plt.show()
data.plot(kind='barh', color='r', alpha=1)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 多组数据
df=pd.DataFrame(np.random.rand(6,4),index=['a','b','c','d','e','f'],columns=pd.Index(['one','two','three','four'],name='Test'))
df.plot.bar()
plt.show()
df.plot.barh(stacked=True,alpha=1)
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame(np.random.rand(6,4),index=['a','b','c','d','e','f'],columns=pd.Index(['one','two','three','four'],name='Test'))
df.plot(kind='bar',stacked=True)
plt.show()