如何将Matplotlib茎叶图顺时针旋转90度?
plt.stem图顺时针旋转90度的实现方法
- 直接交换x、y参数的方式无法满足需求,因为默认plt.stem的茎是垂直于x轴的,需要通过指定茎的方向来实现目标样式。
- 正确代码示例:
import matplotlib.pyplot as plt import numpy as np # 假设m是你的原始数据数组 y_pos = np.arange(m.size) # 用数据索引作为纵向位置 stem_lengths = m # 原始数据作为水平茎的长度 # 绘制水平茎叶图,对应顺时针旋转90度的效果 plt.stem(y_pos, stem_lengths, orientation='horizontal') # 按需调整坐标轴和样式 plt.xlabel('数值') plt.ylabel('数据索引') plt.grid(axis='x', linestyle='--', alpha=0.7) plt.show() - 关键要点:
- 使用
orientation='horizontal'参数,让茎叶图的茎沿水平方向延伸,这就是你要的顺时针旋转90度的效果,和目标图中R(t)的样式一致。 - 之前的尝试只交换了x、y数据,但未修改茎的方向,导致茎仍保持垂直,不符合预期。
- 使用
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

