令人惊讶的是,我没有找到关于如何使用matplotlib.pyplot绘制圆形的直接描述(请不要使用pylab)作为输入中心(x,y)和半径r.我试过这个的一些变种:
import matplotlib.pyplot as plt
circle=plt.Circle((0,0),2)
# here must be something like circle.plot() or not?
plt.show()
Run Code Online (Sandbox Code Playgroud)
......但仍然无法正常工作.
我有一套要点:
a = ([126, 237, 116, 15, 136, 348, 227, 247, 106, 5, -96, 25, 146], [117, 127, 228, 107, 6, 137, 238, 16, 339, 218, 97, -4, -105])
Run Code Online (Sandbox Code Playgroud)
我像这样制作一个散点图:
fig = plt.figure(figsize = (15,6))
ax = fig.add_subplot(111)
ax.scatter(a[0], a[1], color = 'red', s=binradius)
Run Code Online (Sandbox Code Playgroud)
这使得这个情节:
-
我将其与图片重叠,其中每个散点处都有一个球形斑点.我想要适合这个blob,所以我在散点周围定义了一个矩形区域,以便进行拟合.
我想在图上看到这个矩形,以便在视觉上看它们是否足够大以包围blob,如下所示:
我能用scatter吗?或者还有其他方法可以做到吗?
如何更改散点图中标记线的粗细plt.scatter()?markeredgewidth是一个有效的属性plt.plot(); scatter情节是否有任何等效属性?
例如,当您更改散点图的大小(使用marker = 'x')时,标记只会变大,但线条粗细不会改变.
我不是想改变尺寸而是线厚!
我想缩放markersize的matplotlib.pyplot.Axes.scatter基于在x/y轴的点的数量曲线图.
import matplotlib.pyplot as plt
import numpy as np
vmin = 1
vmax = 11
x = np.random.randint(vmin, vmax, 5)
y = np.random.randint(vmin, vmax, 5)
fig, ax = plt.subplots()
for v in np.arange(vmin, vmax):
ax.axvline(v - 0.5)
ax.axvline(v + 0.5)
ax.axhline(v - 0.5)
ax.axhline(v + 0.5)
ax.set_xlim(vmin - 0.5, vmax + 0.5)
ax.set_ylim(vmin - 0.5, vmax + 0.5)
ax.scatter(x, y)
ax.set_aspect(1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
ax始终使用相等的纵横比,两个轴具有相同的lim值.
该markersize两地块留给默认,即markersize=6. …
我正在尝试更改Seaborn factorplots中的标记大小,但我不确定要通过哪个关键字参数
import seaborn as sns
exercise = sns.load_dataset("exercise")
g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise, ci= .95)
Run Code Online (Sandbox Code Playgroud)
我尝试传递基于这些StackOverFlow答案的markersize和s,但似乎都没有效果
我正在尝试用 Python从这里选择一个 R ggplot2 图。我正在查看相关散点图,如下所示。

import pandas as pd
midwest= pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/midwest.csv")
Run Code Online (Sandbox Code Playgroud)
midwest.plot(kind='scatter', x='area', y='poptotal', ylim=((0, 50000)), xlim=((0., 0.1)))
Run Code Online (Sandbox Code Playgroud)
上面的代码本身不会对不同的类别进行颜色编码,而是如下所示。
但是,我们可以按“状态”对数据框进行分组,然后为每个组(ref)单独绘制散点图。
fig, ax = plt.subplots()
groups = midwest.groupby('state')
for name, group in groups:
ax.plot(group.area, group.poptotal, marker='o', linestyle='', ms=10,
label=name)
ax.legend(numpoints=1)
ax.set_ylim((0, 500000))
Run Code Online (Sandbox Code Playgroud)
虽然这确实让我们在散点图中得到了不同的类别,但它并没有让它们的大小增加popdensity.
import seaborn as sns
sns.pairplot(x_vars=["area"], y_vars=["poptotal"], data=midwest,
hue="state", size=5)
plt.gca().set_ylim((0, 50000))
Run Code Online (Sandbox Code Playgroud)
同样,这仅按类别绘制散点图。但是,我们仍然没有标记大小popdensity
下面是我们如何深入到每个数据点并在 Matplotlib 中绘制绘图。
fig, ax = plt.subplots()
groups …Run Code Online (Sandbox Code Playgroud) 我的文本文件中有三列,前两列代表 x 轴和 y 轴位置,我试图在 x 轴和 y 轴处绘制第三列。第三列由 0-5 数字组成。
我已经尝试了以下代码,但它没有给我所需的结果。
import numpy as np
import matplotlib.pyplot as plt
x, y, z = np.loadtxt('test.txt', delimiter=' ', unpack=True)
plt.plot(x, '.', y, '.', z)
plt.xlabel('Distance in meters')
plt.ylabel('Distance in meters')
plt.title('distribution of values')
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)
这是示例 txt 文件。
2411.02 3310.21 3
-1246.65 -3098.39 4
166.298 6042.84 0
----
-51.7214 -2374.09 5
7285.08 661.326 0
-1390.51 4438.9 2
-2741.8 466.014 5
0 0 0
Run Code Online (Sandbox Code Playgroud)
图显示了我从下面的代码中得到的实际图。但是,第三个值应根据 x 轴和 y 轴位置以圆形形式绘制。
这是输出图:
在这些资源的帮助下解决: 在此处输入链接描述 …
我想不使用点或像素而是使用绘图单位来指定散点大小。以下面为例:
import matplotlib.pyplot as plt
x = [0]
y = [0]
plt.scatter(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这会产生如下图:
比如说,如果我希望标记的半径恰好为 0.002 个绘图单位,该怎么办?据我了解,该s参数表示标记大小的区域(以磅为单位)。有没有办法直接以绘图单位提供它?如果不是,从绘图单位转换为点的最佳方法是什么,以便我可以将其传递到s参数中?
我还看到了一些其他问题,建议使用 aPatchCollection并简单地绘制具有指定半径的圆。我不想使用这种方法。我想避免广泛重写现有代码,而是将s参数与plt.scatter.
我尝试使用这个问题中的代码,它显然允许您根据绘图单位控制标记大小。但是我无法真正让它适用于我的基本情况:
import matplotlib.pyplot as plt
import numpy as np
class scatter():
def __init__(self,x,y,ax,size=1,**kwargs):
self.n = len(x)
self.ax = ax
self.ax.figure.canvas.draw()
self.size_data=size
self.size = size
self.sc = ax.scatter(x,y,s=self.size,**kwargs)
self._resize()
self.cid = ax.figure.canvas.mpl_connect('draw_event', self._resize)
def _resize(self,event=None):
ppd=72./self.ax.figure.dpi
trans = self.ax.transData.transform
s = ((trans((1,self.size_data))-trans((0,0)))*ppd)[1]
if s != self.size:
self.sc.set_sizes(s**2*np.ones(self.n))
self.size = s …Run Code Online (Sandbox Code Playgroud) 我正在尝试更新以下函数以通过图例报告集群信息:
color_names = ["red", "blue", "yellow", "black", "pink", "purple", "orange"]
def plot_3d_transformed_data(df, title, colors="red"):
ax = plt.figure(figsize=(12,10)).gca(projection='3d')
#fig = plt.figure(figsize=(8, 8))
#ax = fig.add_subplot(111, projection='3d')
if type(colors) is np.ndarray:
for cname, class_label in zip(color_names, np.unique(colors)):
X_color = df[colors == class_label]
ax.scatter(X_color[:, 0], X_color[:, 1], X_color[:, 2], marker="x", c=cname, label=f"Cluster {class_label}" if type(colors) is np.ndarray else None)
else:
ax.scatter(df.Type, df.Length, df.Freq, alpha=0.6, c=colors, marker="x", label=str(clusterSizes) )
ax.set_xlabel("PC1: Type")
ax.set_ylabel("PC2: Length")
ax.set_zlabel("PC3: Frequency")
ax.set_title(title)
if type(colors) is np.ndarray:
#ax.legend()
plt.gca().legend()
plt.legend(bbox_to_anchor=(1.04,1), loc="upper …Run Code Online (Sandbox Code Playgroud) 我使用matplotlibscatterplot函数在垂直线上创建句柄的外观以描绘图形的某些部分.但是,为了使它们看起来正确,我需要能够将散点图标记对齐到左边(对于左边的线/描绘边)和/或右(对于右边的线/描边器).
这是一个例子:
#create the figure
fig = plt.figure(facecolor = '#f3f3f3', figsize = (11.5, 6))
ax = plt. ax = plt.subplot2grid((1, 1), (0,0))
#make some random data
index = pandas.DatetimeIndex(start = '01/01/2000', freq = 'b', periods = 100)
rand_levels = pandas.DataFrame( numpy.random.randn(100, 4)/252., index = index, columns = ['a', 'b', 'c', 'd'])
rand_levels = 100*numpy.exp(rand_levels.cumsum(axis = 0))
ax.stackplot(rand_levels.index, rand_levels.transpose())
#create the place holder for the vertical lines
d1, d2 = index[25], index[50]
#draw the lines
ymin, ymax = ax.get_ylim() …Run Code Online (Sandbox Code Playgroud)