Tôi đang cố gắng tạo ra một sơ đồ phân tán đơn giản trong pyplot bằng cách sử dụng đối tượng gấu trúc DataFrame, nhưng muốn một cách hiệu quả để vẽ hai biến nhưng có các ký hiệu được chỉ định bởi cột thứ ba [khóa]. Tôi đã thử nhiều cách khác nhau bằng cách sử dụng df.groupby, nhưng không thành công. Một tập lệnh DF mẫu dưới đây. Điều này màu sắc đánh dấu theo 'key1', nhưng id muốn xem một huyền thoại với các danh mục 'key1'. Tôi đang ở gần? Cảm ơn.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame[np.random.normal[10,1,30].reshape[10,3], index = pd.date_range['2010-01-01', freq = 'M', periods = 10], columns = ['one', 'two', 'three']]
df['key1'] = [4,4,4,6,6,6,8,8,8,8]
fig1 = plt.figure[1]
ax1 = fig1.add_subplot[111]
ax1.scatter[df['one'], df['two'], marker = 'o', c = df['key1'], alpha = 0.8]
plt.show[]
hỏi ngày 9 tháng 2 năm 2014 lúc 2:51Feb 9, 2014 at 2:51
0
Bạn có thể sử dụng
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
4 cho việc này, nhưng điều đó đòi hỏi phải có giá trị số cho import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
5 của bạn và bạn sẽ không có huyền thoại, như bạn nhận thấy.Tốt hơn là chỉ sử dụng
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
6 cho các danh mục riêng biệt như thế này. Ví dụ:import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
Nếu bạn muốn mọi thứ trông giống như kiểu
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
7 mặc định, thì chỉ cần cập nhật import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
8 với biểu định kiểu gấu trúc và sử dụng trình tạo màu của nó. [Tôi cũng nhẹ nhàng điều chỉnh huyền thoại]:import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
Đã trả lời ngày 9 tháng 2 năm 2014 lúc 4:23Feb 9, 2014 at 4:23
Joe Kingtonjoe KingtonJoe Kington
264K70 Huy hiệu vàng590 Huy hiệu bạc458 Huy hiệu đồng70 gold badges590 silver badges458 bronze badges
5
Điều này rất đơn giản để làm với Seaborn [
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
9] với tư cách là một onelinerimport matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
0:import seaborn as sns
import pandas as pd
import numpy as np
np.random.seed[1974]
df = pd.DataFrame[
np.random.normal[10, 1, 30].reshape[10, 3],
index=pd.date_range['2010-01-01', freq='M', periods=10],
columns=['one', 'two', 'three']]
df['key1'] = [4, 4, 4, 6, 6, 6, 8, 8, 8, 8]
sns.scatterplot[x="one", y="two", data=df, hue="key1"]
Đây là DataFrame để tham khảo:
Vì bạn có ba cột biến trong dữ liệu của mình, bạn có thể muốn vẽ tất cả các kích thước cặp bằng:
sns.pairplot[vars=["one","two","three"], data=df, hue="key1"]
//rasbt.github.io/mlxtend/user_guide/plotting/carget_scatter/ là một tùy chọn khác.
Đã trả lời ngày 31 tháng 8 năm 2016 lúc 13:44Aug 31, 2016 at 13:44
Bob Baxleybob BaxleyBob Baxley
3,4451 Huy hiệu vàng21 Huy hiệu bạc28 Huy hiệu đồng1 gold badge21 silver badges28 bronze badges
Với
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
1, tôi chỉ có thể nghĩ ra một: sử dụng một nghệ sĩ proxy:df = pd.DataFrame[np.random.normal[10,1,30].reshape[10,3], index = pd.date_range['2010-01-01', freq = 'M', periods = 10], columns = ['one', 'two', 'three']]
df['key1'] = [4,4,4,6,6,6,8,8,8,8]
fig1 = plt.figure[1]
ax1 = fig1.add_subplot[111]
x=ax1.scatter[df['one'], df['two'], marker = 'o', c = df['key1'], alpha = 0.8]
ccm=x.get_cmap[]
circles=[Line2D[range[1], range[1], color='w', marker='o', markersize=10, markerfacecolor=item] for item in ccm[[array[[4,6,8]]-4.0]/4]]
leg = plt.legend[circles, ['4','6','8'], loc = "center left", bbox_to_anchor = [1, 0.5], numpoints = 1]
Và kết quả là:
Đã trả lời ngày 9 tháng 2 năm 2014 lúc 4:19Feb 9, 2014 at 4:19
CT ZHuct ZhuCT Zhu
50.3K17 Huy hiệu vàng114 Huy hiệu bạc131 Huy hiệu đồng17 gold badges114 silver badges131 bronze badges
0
Bạn có thể sử dụng df.plot.scatter và chuyển một mảng cho c = đối số xác định màu của mỗi điểm:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame[np.random.normal[10,1,30].reshape[10,3], index = pd.date_range['2010-01-01', freq = 'M', periods = 10], columns = ['one', 'two', 'three']]
df['key1'] = [4,4,4,6,6,6,8,8,8,8]
colors = np.where[df["key1"]==4,'r','-']
colors[df["key1"]==6] = 'g'
colors[df["key1"]==8] = 'b'
print[colors]
df.plot.scatter[x="one",y="two",c=colors]
plt.show[]
Đã trả lời ngày 17 tháng 9 năm 2017 lúc 2:45Sep 17, 2017 at 2:45
Arjaan Buijkarjaan BuijkArjaan Buijk
1.14112 Huy hiệu bạc17 Huy hiệu đồng12 silver badges17 bronze badges
Từ matplotlib 3.1 trở đi, bạn có thể sử dụng
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
2. Một ví dụ được hiển thị trong sáng tạo huyền thoại tự động. Ưu điểm là một cuộc gọi phân tán duy nhất có thể được sử dụng.Trong trường hợp này:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame[np.random.normal[10,1,30].reshape[10,3],
index = pd.date_range['2010-01-01', freq = 'M', periods = 10],
columns = ['one', 'two', 'three']]
df['key1'] = [4,4,4,6,6,6,8,8,8,8]
fig, ax = plt.subplots[]
sc = ax.scatter[df['one'], df['two'], marker = 'o', c = df['key1'], alpha = 0.8]
ax.legend[*sc.legend_elements[]]
plt.show[]
Trong trường hợp các khóa không được đưa ra trực tiếp dưới dạng số, nó sẽ trông như
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame[np.random.normal[10,1,30].reshape[10,3],
index = pd.date_range['2010-01-01', freq = 'M', periods = 10],
columns = ['one', 'two', 'three']]
df['key1'] = list["AAABBBCCCC"]
labels, index = np.unique[df["key1"], return_inverse=True]
fig, ax = plt.subplots[]
sc = ax.scatter[df['one'], df['two'], marker = 'o', c = index, alpha = 0.8]
ax.legend[sc.legend_elements[][0], labels]
plt.show[]
Đã trả lời ngày 8 tháng 6 năm 2019 lúc 14:42Jun 8, 2019 at 14:42
3
Bạn cũng có thể thử Altair hoặc GGPOT tập trung vào trực quan khai báo.
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
Mã Altair
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
0Mã GGPLOT
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
1Đã trả lời ngày 3 tháng 7 năm 2017 lúc 9:19Jul 3, 2017 at 9:19
Nipun Batranipun BatraNipun Batra
10,5K10 Huy hiệu vàng51 Huy hiệu bạc74 Huy hiệu đồng10 gold badges51 silver badges74 bronze badges
Nó khá hacky, nhưng bạn có thể sử dụng
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
3 như một import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
4 để làm mọi thứ trong một lần:import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
2Lưu ý rằng kể từ 0,20.3, việc sắp xếp chỉ số là cần thiết và truyền thuyết này hơi khó khăn.
Đã trả lời ngày 21 tháng 10 năm 2017 lúc 20:51Oct 21, 2017 at 20:51
Fugledefugledefuglede
16.4K2 Huy hiệu vàng54 Huy hiệu bạc90 Huy hiệu Đồng2 gold badges54 silver badges90 bronze badges
Seaborn có chức năng trình bao bọc
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
plt.rcParams.update[pd.tools.plotting.mpl_stylesheet]
colors = pd.tools.plotting._get_standard_colors[len[groups], color_type='random']
fig, ax = plt.subplots[]
ax.set_color_cycle[colors]
ax.margins[0.05]
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[numpoints=1, loc='upper left']
plt.show[]
5 làm điều đó hiệu quả hơn.import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed[1974]
# Generate Data
num = 20
x, y = np.random.random[[2, num]]
labels = np.random.choice[['a', 'b', 'c'], num]
df = pd.DataFrame[dict[x=x, y=y, label=labels]]
groups = df.groupby['label']
# Plot
fig, ax = plt.subplots[]
ax.margins[0.05] # Optional, just adds 5% padding to the autoscaling
for name, group in groups:
ax.plot[group.x, group.y, marker='o', linestyle='', ms=12, label=name]
ax.legend[]
plt.show[]
3Đã trả lời ngày 8 tháng 10 năm 2020 lúc 9:10Oct 8, 2020 at 9:10
yosemite_kyosemite_kyosemite_k
2.9741 Huy hiệu vàng15 Huy hiệu bạc26 Huy hiệu đồng1 gold badge15 silver badges26 bronze badges