Python - 坐标绘图
Python - coordinates plotting
我需要建议。在我的数据库中,我有 x 点和 y 点的形式:
df = pd.read_sql_table('table', cnx)
id
curves
1
[(x,y),(x,y),(x,y) etc.]
2
[(x,y),(x,y),(x,y) etc.]
type(df['curves']) #pandas.core.series.Series
我想尝试绘制折线图。
我尝试了几种解决方案。但对我没有任何帮助。
data = df['curves']
plt.plot(*zip(*data))
plt.show()
或
data_in_array = np.array(data)
transposed = data_in_array.T
x,y = transposed #but here i got error - not enough values to unpack (expected 2, got 1)
请指教
展开曲线列表列
df = df.explode('curves').reset_index()
创建单独的 x 和 y 列
df['curves_x'] = df.curves.str[0]
df['curves_y'] = df.curves.str[1]
使用 seaborn 绘图
import seaborn as sns
import matplotlib.pyplot as plt
sns.lineplot(data=df, x='curves_x', y='curves_y', hue='id')
plt.show()
为了更好地理解,我们来看一些示例数据,
id curves
0 1 [(10, 20), (20, 30), (30, 50)]
1 2 [(10, 10), (20, 40), (30, 20)]
展开列并重置索引
id curves
0 1 (10, 20)
1 1 (20, 30)
2 1 (30, 50)
3 2 (10, 10)
4 2 (20, 40)
5 2 (30, 20)
创建 x 和 y 列
id curves curves_x curves_y
0 1 (10, 20) 10 20
0 1 (20, 30) 20 30
0 1 (30, 50) 30 50
1 2 (10, 10) 10 10
1 2 (20, 40) 20 40
1 2 (30, 20) 30 20
剧情会是这样
我需要建议。在我的数据库中,我有 x 点和 y 点的形式:
df = pd.read_sql_table('table', cnx)
id | curves |
---|---|
1 | [(x,y),(x,y),(x,y) etc.] |
2 | [(x,y),(x,y),(x,y) etc.] |
type(df['curves']) #pandas.core.series.Series
我想尝试绘制折线图。
我尝试了几种解决方案。但对我没有任何帮助。
data = df['curves']
plt.plot(*zip(*data))
plt.show()
或
data_in_array = np.array(data)
transposed = data_in_array.T
x,y = transposed #but here i got error - not enough values to unpack (expected 2, got 1)
请指教
展开曲线列表列
df = df.explode('curves').reset_index()
创建单独的 x 和 y 列
df['curves_x'] = df.curves.str[0]
df['curves_y'] = df.curves.str[1]
使用 seaborn 绘图
import seaborn as sns
import matplotlib.pyplot as plt
sns.lineplot(data=df, x='curves_x', y='curves_y', hue='id')
plt.show()
为了更好地理解,我们来看一些示例数据,
id curves
0 1 [(10, 20), (20, 30), (30, 50)]
1 2 [(10, 10), (20, 40), (30, 20)]
展开列并重置索引
id curves
0 1 (10, 20)
1 1 (20, 30)
2 1 (30, 50)
3 2 (10, 10)
4 2 (20, 40)
5 2 (30, 20)
创建 x 和 y 列
id curves curves_x curves_y
0 1 (10, 20) 10 20
0 1 (20, 30) 20 30
0 1 (30, 50) 30 50
1 2 (10, 10) 10 10
1 2 (20, 40) 20 40
1 2 (30, 20) 30 20
剧情会是这样