- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我只想通过点来绘制简单的形状,像这样:
import matplotlib.pyplot as plt
rectangle = [(0,0),(0,1),(1,1),(1,0)]
hexagon = [(0,0),(0,1),(1,2),(2,1),(2,0),(1,-1)]
l_shape = [(0,0),(0,3),(1,3),(1,1),(3,1),(3,0)]
concave = [(0,0),(0,3),(1,3),(1,1),(2,1),(2,3),(3,3),(3,0)]
for points in [rectangle, hexagon, l_shape, concave]:
# 1. Can I get rid of the zip? plot directly by points
# 2. How can I make the shape complete?
xs, ys = zip(*points)
plt.plot(xs, ys, 'o')
plt.plot(xs, ys, '-')
automin, automax = plt.xlim()
plt.xlim(automin-0.5, automax+0.5)
automin, automax = plt.ylim()
plt.ylim(automin-0.5, automax+0.5)
# Can I display the shapes 2 in 1 line?
plt.show()
我的问题是
*zip
?我的意思是,直接按点绘制,而不是 2 个数组。完整
?由于我正在遍历所有点,第一个和最后一个不能连接在一起,我该怎么做?convex hull
?)最佳答案
要关闭形状,只需在列表末尾再次添加第一个点:
# rectangle = [(0,0),(0,1),(1,1),(1,0)]
rectangle = [(0,0),(0,1),(1,1),(1,0),(0,0)]
plt.plot
获取 x 坐标列表和 y 坐标列表。我会说你现在做的方式已经是“按点而不是 2 个数组”做的方式。因为如果你想在没有 zip
的情况下完成它,它看起来像这样:
rectangleX = [0, 0, 1, 1, 0]
rectangleY = [0, 1, 1, 0, 0]
plt.plot(rectangleX, rectangleY, 'o')
plt.plot(rectangleX, rectangleY, '-')
更新:
为了获得更好的多边形支持,请使用 patches模块 [ example ].这可能更符合您正在寻找的内容。默认情况下 (closed = True
),它将为您关闭路径,并且还允许您将顶点直接添加到列表(而不是两个单独的列表):
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
rectangle = [(0,0),(0,1),(1,1),(1,0)]
fig, ax = plt.subplots()
ax.add_patch(mpatches.Polygon(rectangle))
automin, automax = plt.xlim()
plt.xlim(automin-0.5, automax+0.5)
automin, automax = plt.ylim()
plt.ylim(automin-0.5, automax+0.5)
plt.show()
关于Python 和 Matplot : How can I draw a simple shape by points?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250509/
我正在尝试使用 matplot 来绘制由不同输入生成的特定分钟内的事件数。 与下面的屏幕剪辑大致相似,但仅显示了一个简单的 24 小时时段。 我的部分问题是我不知道如何处理不完整的 Y 轴数据。例如,
我正在尝试使用 matplot 库创建条形图,但我无法弄清楚该函数的参数是什么。 文档说 bar(left, height),但我不知道如何在此处输入我的数据 [这是一个名为 x 的数字列表]。 它告
我使用 matplot 在图表上绘制了几条线: matplot(cumsum(as.data.frame(daily.pnl)),type="l") 这为我提供了每行的默认颜色 - 这很好, 但我现在
当我点击其图例标签时,任何人都可以帮助如何更改 pyplot 中一组点的颜色? 下面是一段代码,其中有两组点 x1 和 x2。我将它们绘制成散点图。它以蓝色绘制集合 x1 和 x2 中的所有点。当我点
我有一个像下面这样的简单文件,上面有一些文本和数字: some text pre 69 some text post some text pre 63 some text post some text
我希望创建一个条形之间没有间隙的直方图。尝试添加参数 bin 或 range 但没有成功。 import numpy as np from matplotlib import pyplot as pl
我在 R 中创建了一个并排图,其中两个图应该使用相同的 y Axis 。然而,左边的图是一个 boxplot ,右边的图是一个 matplot 并且在两个图中我设置了相同的 y Axis 范围 yli
我正在尝试以 -20 到 + 20 的序列创建 y 轴标签,增量为 5 个整数。我在下面用我的绘图 axis = ax2 尝试了这个: ax2.set_yticks(list(range(-20, 2
我正在尝试将 matlab 代码转换为 python。这是我写的Python代码 import numpy as np import numpy.matlib as nm import matplot
我正在使用 matplotlib 来绘制二维 numpy 数组和 bin 的黑白图。在此之前,我只使用两个列表绘制图表。在直方图中,将有三个堆叠条形,对应于数组 a 中的列数。我想知道命令 plt.h
我想调用matplotlib的draw()方法,用于更新已更改但未自动重新绘制的图形。请注意,该图是在 QT 应用程序内部构建的。以下是错误跟踪: Traceback (most recent cal
我有一个名字、州、年份、性别和名字出现次数的列表。我正在尝试绘制多年来所有州合并的给定名称。 allyears.head() 结果如下: name sex number year
我有一本这样的字典,想要可视化某个主播的在线时间。 {1: ['2018-09-20 20:40:50', '2018-09-20 21:11:14'], 2: ['2018-09-20 12:45:
这个问题已经有答案了: pyplot scatter plot marker size (6 个回答) 已关闭 3 年前。 我有一个散点图,其中 X 轴是纬度,Y 轴是经度。每个点代表一家餐厅。标记大
我正在尝试根据我的值绘制直方图 x = ['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '2', '3', '4', '5',
我正在尝试更改 matplot 中的 x 轴,但此命令不起作用: TimePoints=1997:2011 matplot(t(DataMatrix),type='l',col="black",lwd
我正在尝试将我的 3D 三曲面图保存为交互式 HTML 图,因此应该可以放大/缩小并更改视点。在 IDE 中, plotly 已经存在并且到目前为止有效,但我由于 ValueError,无法将其保存为
我想在 matplot 上绘制散点趋势线。我怎样才能做到这一点? Python import pandas as pd import matplotlib.pyplot as plt csv = pd
我想在同一图中将不同的行绘制为不同的线,以说明 3 个组的平均发展趋势:All、Men 和 Women。但是,我没有打印出其中一行,并且图例中没有填充行名。 我很高兴在 matplot 中找到解决方案
这是 question 的后续内容。 看下面的例子: import pandas as pd import matplotlib.pyplot as plt import numpy as np d
我是一名优秀的程序员,十分优秀!