目录
1. legend 控制
功能:将图例放在图的外面
# legend
fig.legend(bbox_to_anchor=(0,1.02,1,0.2), loc="lower center",
mode="expand", borderaxespad=1, ncol=3,fontsize=20)
bbox_to_anchor 放置图例的框
loc 位置
How to put the legend out of the plot
2. 线条控制
线条类型:
字符|类型 | 字符|类型
---|--- | --- | ---
` '-' `| 实线 | `'--'`| 虚线
`'-.'`| 虚点线 | `':'`| 点线
`'.'`| 点 | `','`| 像素点
`'o'` |圆点 | `'v'`| 下三角点
`'^'`| 上三角点 | `'<'`| 左三角点
`'>'`| 右三角点 | `'1'`| 下三叉点
`'2'`| 上三叉点 | `'3'`| 左三叉点
`'4'`| 右三叉点 | `'s'`| 正方点
`'p'` | 五角点 | `'*'`| 星形点
`'h'`| 六边形点1 | `'H'`| 六边形点2
`'+'`| 加号点 | `'x'`| 乘号点
`'D'`| 实心菱形点 | `'d'`| 瘦菱形点
`'_'`| 横线点 | |
线条粗细:
linewidth参数控制
3. 透明度控制
alpha参数控制
完整栗子1:
画图前,先导入matplotlib包:
import matplotlib.pyplot as plt
def draw_path_jpg_two_curves(x, y1, y2,y3,label1, label2, label3, file_name):
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
plt.switch_backend('agg')
# t = datetime
fig, ax1 = plt.subplots(figsize=(20, 10)) # figsize调整图片的大小
color = 'tab:orange'
ax1.set_xlabel('Month',fontsize=20)
ax1.set_ylabel('ylable1', color=color,fontsize=20)
ax1.plot(x, y1, color=color,label = label1, linewidth=5,marker = 'o',linestyle=':',markersize = 10)
ax1.plot(x, y3, color='b',label = label3, linewidth=5,marker = 'o',linestyle=':',markersize = 10)
ax1.tick_params(axis='y', labelcolor=color,labelsize=20)
ax1.tick_params(axis='x', labelsize=20)
ax2 = ax1.twinx() # 创建共用x轴的第二个y轴
color = 'tab:purple'
ax2.set_ylabel('ylable2', color=color,fontsize=20)
ax2.bar(x, y2, color=color,alpha=0.5,label=label2)
ax2.tick_params(axis='y', labelcolor=color,labelsize=20)
# 设置等间隔x轴
tick_spacing = 25
ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax2.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
# legend
fig.legend(bbox_to_anchor=(0,1.02,1,0.2), loc="lower center",
mode="expand", borderaxespad=1, ncol=3,fontsize=20)
fig.tight_layout()
plt.xticks(x, ['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.'], rotation='vertical',fontsize=20)
plt.show() # 直接显示出来
x = np.arange(0, 12, 1)
label1 = 'a'
labal2 = 'b'
label3 = 'c'
draw_path_jpg_two_curves(x, y, y1, y2,label1, labal2, label3,'test')
完整栗子2:
def draw_path_jpg_two_curves(x, y1, y2, label1, label2, file_name):
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
plt.switch_backend('agg')
# t = datetime
fig, ax1 = plt.subplots(figsize=(20, 10)) # figsize调整图片的大小
color = 'tab:orange'
ax1.set_xlabel('time (s)')
ax1.set_ylabel(label1, color=color)
ax1.plot(x, y1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # 创建共用x轴的第二个y轴
color = 'tab:purple'
ax2.set_ylabel(label2, color=color)
ax2.plot(x, y2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
# 设置等间隔x轴
tick_spacing = 25
ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax2.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
fig.tight_layout()
# plt.savefig('./' + file_name) # 保存图片
plt.show() # 直接显示出来
# plt.close()
x = np.arange(0, 100, 1)
y1 = np.sin(x)
y2 = np.log1p(x)
label1 = 'sin'
labal2 = 'log'
draw_path_jpg_two_curves(x, y1, y2, label1, labal2, 'test')