向任意位置添加坐标轴
- add_axes/axes(arg=(None【subplot(111)】/4-tuple【四个0-1的浮点型元组left/bottom确定坐标轴的位置;width/hright确定坐标轴的大小】frame_on是否绘制每个坐标轴的轴脊)
代码示例
import matplotlib.pyplot as plt
ax = plt.axes((0.2, 0.5,0.3,0.3))
ax.plot([1,2,3,4])
ax1 = plt.axes((0.6, 0.4,0.2,0.2))
ax1.plot([1,2,3,4])
plt.show()
定制刻度
代码示例
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
from matplotlib.dates import DateFormatter, HourLocator
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
dates = ['201910240', '2019102402', '2019102404', '2019102406', '2019102408', '2019102410', '2019102412',
'2019102414', '2019102416', '2019102418', '2019102420', '2019102422', '201910250']
x_data = [datetime.strptime(d, '%Y%m%d%H') for d in dates]
y_data = np.array([7, 9, 11, 14, 8, 15, 22, 11, 10, 11, 11, 13, 8])
fig = plt.figure()
ax = fig.add_axes((0.0, 0.0, 1.0, 1.0))
ax.plot(x_data, y_data, '->', ms = 8, mfc = '#FF9900')
ax.set_title("深证24小时平均风速2020080603002")
ax.set_ylabel('时间')
ax.set_xlabel('平均风速(km/h)')
data_fmt = DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(data_fmt)
ax.xaxis.set_major_locator(HourLocator(interval = 2))
ax.tick_params(direction='in', length=6, width=2, labelsize=12)
ax.xaxis.set_tick_params(labelrotation=45)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
plt.show()
?
隐藏轴脊
axis(option【true/on:显示,False/off:隐藏】)
代码示例1
import matplotlib.pyplot as plt
import numpy as np
# 绘制多边形
import matplotlib.patches as mpathes //绘制其他形状具体参照此库
polygon = mpathes.RegularPolygon((0.5, 0.5)//中心点, 6//多边形顶点数量, 0.2//半径, color = 'g')
ax = plt.axes((0.3, 0.3, 0.5, 0.5))
ax.add_patch(polygon)
ax.axis('off')
plt.show()
?代码示例2:隐藏部分轴脊
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpathes
polygon = mpathes.RegularPolygon((0.5, 0.5), 6, 0.2, color = 'g')
ax = plt.axes((0.3, 0.3, 0.5, 0.5))
ax.add_patch(polygon)
ax.spines['top'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.yaxis.set_ticks_position('none')
ax.set_yticklabels([])
plt.show()
?
移动轴脊
set_position()
?代码示例
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpathes
polygon = mpathes.RegularPolygon((0.5, 0.5), 6, 0.2, color = 'g')
ax = plt.axes((0.3, 0.3, 0.5, 0.5))
ax.add_patch(polygon)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0.5))
ax.spines['bottom'].set_position(('data', 0.5))
plt.show()
?
?
|