python画图库-matplotlib
直线图
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
柱状图
from matplotlib import pyplot as plt
import matplotlib
# fname 为 你下载的字体库路径,注意 SimHei.ttf 字体的路径
zhfont1 = matplotlib.font_manager.FontProperties(fname="SimHei.ttf")
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置显示中文
plt.rcParams['axes.unicode_minus'] = False
#n = ['一月','二月','三月','四月','五月','六月']
y = [100,250,200,350,210,500]
#x = list(range(len(n)) )
#plt.xticks(x,n,rotation=90)
x = ['一月','二月','三月','四月','五月','六月']
plt.bar(x, y)
plt.title('前6个月的产量',fontproperties=zhfont1,fontsize=20)
plt.ylabel('数量/万',fontproperties=zhfont1)
plt.xlabel('时间/月份',fontproperties=zhfont1)
plt.show()
折线图
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
# fname 为 你下载的字体库路径,注意 SimHei.ttf 字体的路径
zhfont1 = matplotlib.font_manager.FontProperties(fname="SimHei.ttf")
matplotlib.rcParams['font.sans-serif'] = ['Microsoft YaHei']
x = ['1号','2号','3号','4号','5号','6号','7号','8号','9号','10号']
y = [33,14,22,34,21,34,33,38,19,24]
plt.title("1-10号天气情况",fontproperties=zhfont1)
plt.xlabel("时间/天",fontproperties=zhfont1)
plt.ylabel("温度/。C",fontproperties=zhfont1)
plt.plot(x, y,color='blue', linewidth=1.0)
my_x_ticks = np.arange(0, 10, 1)
plt.show()
散点图
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import numpy as np
xs = np.arange(7)
ys = xs**2
fig = plt.figure(figsize=(5, 10))
ax = plt.subplot(2, 1, 1)
# If we want the same offset for each text instance,
# we only need to make one transform. To get the
# transform argument to offset_copy, we need to make the axes
# first; the subplot command above is one way to do this.
trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
x=0.05, y=0.10, units='inches')
for x, y in zip(xs, ys):
plt.plot(x, y, 'ro')
plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset)
# offset_copy works for polar plots also.
ax = plt.subplot(2, 1, 2, projection='polar')
trans_offset = mtransforms.offset_copy(ax.transData, fig=fig,
y=6, units='dots')
for x, y in zip(xs, ys):
plt.polar(x, y, 'ro')
plt.text(x, y, '%d, %d' % (int(x), int(y)),
transform=trans_offset,
horizontalalignment='center',
verticalalignment='bottom')
plt.show()
参数
zhfont1 = matplotlib.font_manager.FontProperties(fname=“SimHei.ttf”)#当需要中文时需要引入字体
matplotlib.rcParams[‘font.sans-serif’] = [‘Microsoft YaHei’]#当x或y含有字符串
plt.rcParams[‘axes.unicode_minus’] = False
参数 | 值 | 参数 | 值 |
---|---|---|---|
‘’-‘’ | 实线样式 | ‘’–‘’ | 短横线样式 |
“-.” | 点划线样式 | “:” | 虚线样式 |
“.” | 点标记 | “,” | 像素标记 |
“o” | 圆标记 | ||
“b” | 蓝色 | ”g“ | 绿色 |