使用Plotly创建笛卡尔平面直角坐标系

3
这使用matplotlib创建了我所说的图表:
import matplotlib.pyplot as plt
import numpy as np

xmin, xmax, ymin, ymax = -9, 9, -9, 9
fig, ax = plt.subplots(figsize=(20, 20))

ax.set(xlim=(xmin - 1, xmax + 1), ylim=(ymin - 1, ymax + 1), aspect='equal')

ax.spines['bottom'].set(position="zero", linewidth=2.5)
ax.spines['left'].set(position="zero", linewidth=2.5)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.text(10.15, 0, "x", fontdict=font, va="center")
ax.text(0, 10.15, "y", fontdict=font, ha="center")

x_ticks = np.arange(xmin, xmax)
y_ticks = np.arange(ymin, ymax)

ax.set_xticks(x_ticks[x_ticks != x_ticks])
ax.set_yticks(y_ticks[y_ticks != y_ticks])

ax.set_xticks(np.arange(xmin, xmax+1), minor=True)
ax.set_yticks(np.arange(ymin, ymax+1), minor=True)
ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.25)

plt.show()

输出:如下所示的二维笛卡尔平面enter image description here

是否可能使用plotly获得类似的结果?


2D平面周围有边界吗? - Hamzah
1个回答

2
这是在Plotly中如何完成的:
import plotly.graph_objects as go

axis_range = [-9,9]

fig = go.Figure()

fig.update_xaxes(range=axis_range,title = 'y', tickmode = 'linear',
                 showticklabels = False, side='top',gridcolor="rgb(224,224,224)")

fig.update_yaxes(range=axis_range,title = 'x', tickmode = 'linear',
                 showticklabels = False, side='right', gridcolor="rgb(224,224,224)")

fig.add_vline(x=0, line_width=3)
fig.add_hline(y=0, line_width=3)



fig.update_layout(plot_bgcolor='rgb(255,255,255)', height=800, width=800)

fig.show()

enter image description here

这里唯一的缺点是在Plotly中无法旋转x轴标签,如此处所述。

谢谢!不过,为什么X轴比Y轴长呢? - Adryan Zeigler
我将高度和宽度设置为相同的大小,问题就解决了。 - Hamzah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接