对坐标轴的操作
plt.gca()
将左边的轴移到中间,上轴和右轴都没有了
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(-50,51)
y=x**2
ax=plt.gca() #获取坐标轴 通过坐标轴spines 确定 top bottom left right
ax.spines['right'].set_color("none") none 代表没有这个坐标轴
ax.spines['top'].set_color("none")
ax.spines['left'].set_position(('axes',0.5)) axes 整个轴上的比例 data 挪动到哪的刻度值
plt.ylim(0,2500) #设置坐标区间
plt.plot(x,y)
设置图形分辨率和大小
图表的样式参数设置
线条样式 marker=“ ”
创建图形对象
fig=plt.figure()
ax1=fig.add_axes([0,0,1,1])
ax2=fig.add_axes([0.2,0.2,0.5,0.5])
ax1.plot([1,2,3,4],[2,3,4,5])
调用区域点plot() 不可以直接用fig.plot()
绘制对个子图
add_axes(): 添加区域 在不同区域作图
subplot(): 均等划分画布
subplots(): 包含子图区域的画布,和figure对象
axes:分一些比较常规的区域画布
subplot:不规则的
subplot()
柱状图
plt.bar()
bottom 柱状图的起始位置
import matplotlib.pyplot as plt
import numpy as np
countries=['诺威','德国','中国','美国','瑞典']
gold=[16,12,9,8,5]
silver=[8,10,4,10,5]
brozen=[7,9,6,8,5]
x=np.arange(len(countries)) #将x轴转换为数值
width=0.2
gold_x=x
silver_x=x+width
brozen_x=x+2*width
#plt.title("基本柱状图")
#plt.grid(ls="--",alpha=0.5)
plt.bar(gold_x,gold,width=width,color="gold")
# color 可以多个颜色color=["r","b"]
plt.bar(silver_x,silver,width=width,color="silver")
plt.bar(brozen_x,brozen,width=width,color="brown")
#将x标签变回来
#让x标签位置居中
plt.xticks(x+width,labels=countries)
#显示高度文本
for i in range(len(countries)):
plt.text(gold_x[i],gold[i],gold[i],va="bottom",ha="center")
plt.text(silver_x[i],silver[i],silver[i],va="bottom",ha="center")
plt.text(brozen_x[i],brozen[i],brozen[i],va="bottom",ha="center")
其他图形
plt.barh 水平柱状图 plt.hist 直方图 plt.pie()饼图 散点图 plt.scatter
散点图