我想修改茎图的轴,例如在主干图example中用ax.grid(color='gray', axis='y')
更改网格颜色。我该怎么办?
import matplotlib.pyplot as plt
import numpy as np
# returns 10 evenly spaced samples from 0.1 to 2*PI
x = np.linspace(0.1, 2 * np.pi, 10)
markerline, stemlines, baseline = plt.stem(x, np.cos(x), '-.')
# setting property of baseline with color red and linewidth 2
plt.setp(baseline, color='r', linewidth=2)
答案 0 :(得分:1)
答案 1 :(得分:0)
首先使用plt.subplots()
定义轴,然后使用ax.stem
而不是plt.stem
进行调用:
fig, ax = plt.subplots()
markerline, stemlines, baseline = ax.stem(x, np.cos(x), '-.')
ax.grid(color='gray', axis='y')
答案 2 :(得分:0)
对于How to get a matplotlib Axes instance to plot to?,另一种选择是在末尾添加:
ax = plt.gca()
ax.grid(color='gray', axis='y')
(ht ImportanceOfBeingErnest)