matplotlib中的数组列数据太多图例 [英] too many legend with array column data in matplotlib

查看:76
本文介绍了matplotlib中的数组列数据太多图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用列表数据绘制简单的旋转矩阵结果.但是我的带有结果数组的图形具有与屏幕转储图像一样多的索引.第二个图与我的属性(线条样式等)不完全相同我想我确实在绘制数组时出错,但是不知道是什么.欢迎任何意见.预先感谢.

我的代码如下.

 将numpy导入为np导入matplotlib.pyplot作为plttheta = np.radians(30)c,s = np.cos(theta),np.sin(theta)R = np.matrix('{} {}; {} {}'.format(c,-s,s,c))x = [-9,-8,-7,-6,-5,-4,-3,-2,-1、1、2、3、4、5、6、7、8、9]y = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]line_b = [x,y]result_a = R * np.array(line_b)图= plt.figure()ax1 = fig.add_subplot(111)plt.plot(line_b [0],line_b [1],color ="blue",linewidth = 2.5,linestyle =-",label ='measured')plt.plot(result_a [0],result_a [1],'r *-',label ='rotated')ax1.set_ylim(-10,10)ax1.set_xlim(-10,10)plt.legend()#轴中心移动0,0ax1.spines ['right'].set_color('none')ax1.spines ['top'].set_color('none')ax1.xaxis.set_ticks_position('底部')ax1.spines ['bottom'].set_position(('data',0))ax1.yaxis.set_ticks_position('左')ax1.spines ['left'].set_position(('data',0))plt.show() 

解决方案

问题是,您试图绘制 result_a 的两行,就好像它们是一维的 np.ndarray ,实际上它们是 np.matrix ,它们始终是二维的.亲自看看:

 >>>result_a [0] .shape(1,19) 

要解决此问题,您需要将向量 result_a [0],result_a [1] 转换为数组.在此答案中

I try to plot simple rotation matrix result with list data. but My figure with result array have so many index as screen dump image. and the second plot is not exact with my attribute(line style, etc.) I guess that I do mistake array handling to plot but don't know what. Any comments are welcome. Thanks in advance.

My code is below.

import numpy as np
import matplotlib.pyplot as plt

theta = np.radians(30)
c, s = np.cos(theta), np.sin(theta)
R = np.matrix('{} {}; {} {}'.format(c, -s, s, c))
x = [-9, -8, -7, -6, -5, -4, -3, -2, -1,0,1,2,3,4,5,6,7,8,9]
y = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

line_b = [x,y]

result_a = R*np.array(line_b)

fig=plt.figure()
ax1 = fig.add_subplot(111)
plt.plot(line_b[0],line_b[1], color="blue", linewidth=2.5, linestyle="-",    label='measured')
plt.plot(result_a[0], result_a[1], 'r*-', label='rotated')
ax1.set_ylim(-10,10)
ax1.set_xlim(-10,10)
plt.legend()

# axis center to move 0,0
ax1.spines['right'].set_color('none')
ax1.spines['top'].set_color('none')
ax1.xaxis.set_ticks_position('bottom')
ax1.spines['bottom'].set_position(('data',0))
ax1.yaxis.set_ticks_position('left')
ax1.spines['left'].set_position(('data',0))

plt.show()

解决方案

The issue is that you are trying to plot the two rows of result_a as if they were 1-dimensional np.ndarrays, when in fact they are np.matrix which are always 2-dimensional. See for yourself:

>>> result_a[0].shape
(1, 19)

To remedy this, you need to convert your vectors result_a[0], result_a[1] to arrays. Simple ways can be found in this answer. For example,

rx = result_a[0].A1
ry = result_a[1].A1
# alternatively, the more compact
# rx, ry = np.array(result_a)
plt.plot(rx, ry, 'r*-', label='rotated')

yields the following (with plt.legend(); plt.show()):

这篇关于matplotlib中的数组列数据太多图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆