简体   繁体   English

使用python绘图时缺少X和Y轴

[英]Missing X and Y axis when plotting using python

I having some issue replicating the example code for scatter plots in python. 我在复制python中的散点图示例代码时遇到一些问题。 The code I am trying to replicate: 我正在尝试复制的代码:

x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)

plt.subplot(321)
plt.scatter(x, y, s=80, c=z, marker=">")

plt.subplot(322)
plt.scatter(x, y, s=80, c=z, marker=(5, 0))

verts = list(zip([-1., 1., 1., -1.], [-1., -1., 1., -1.]))
plt.subplot(323)
plt.scatter(x, y, s=80, c=z, marker=(verts, 0))
# equivalent:
#plt.scatter(x,y,s=80, c=z, marker=None, verts=verts)

plt.subplot(324)
plt.scatter(x, y, s=80, c=z, marker=(5, 1))

plt.subplot(325)
plt.scatter(x, y, s=80, c=z, marker='+')

plt.subplot(326)
plt.scatter(x, y, s=80, c=z, marker=(5, 2))

plt.show()

What I am expecting: 我期望的是: 在此处输入图片说明

But this is what I am getting: 但这就是我得到的: 在此处输入图片说明

I can get the background to white by: 我可以通过以下方式将背景设置为白色:

ax = plt.gca()
ax.set_axis_bgcolor('white')

But I can not get to display X and Y axis. 但是我无法显示X和Y轴。 I have tried many solutions from other stackoverflow posts but none of them seems to be working. 我已经尝试了其他stackoverflow帖子中的许多解决方案,但似乎都没有用。

Could someone please help me solve this issue? 有人可以帮我解决这个问题吗?

It seems that you are using ggplot style. 看来您使用的是ggplot样式。 You can change the default style: 您可以更改默认样式:

pl.style.use("ggplot")
pl.rcParams['axes.edgecolor'] = "#777777"
pl.rcParams['axes.facecolor'] = '#FFFFFF'

Here is the result: 结果如下:

在此处输入图片说明

Thanks to @HYRY's solution. 感谢@HYRY的解决方案。

Some more references about style sheets in matplotlib for beginners like me: 对于像我这样的初学者,更多有关matplotlib中样式表的参考:

  1. Customizing plots with style sheets 使用样式表自定义图

    • Defining your own style 定义自己的风格
    • Composing styles 构图风格
    • Temporary styling 临时样式
  2. Matplotlib Style Gallery Matplotlib样式库

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绘制x和y辅助轴时缺少一个辅助标签 - One secondary label missing when plotting x and y secondary axis 当使用 matplotlib 从 CSV 文件绘制时,X、Y 轴上的 0 值未绘制,我需要绘制它们 - When plotting from a CSV file using matplotlib, 0 values on X, Y axis are not plotting and I need them to be plotted 使用python在同一图中为csv dile绘制多个x轴和多个y轴 - Plotting multiple x axis and multiple y axis in same graph for csv dile using python 在 x 和 y 轴上绘制符号 - plotting with symbols on x and y axis 如何指定x和y轴以在Python中绘制数据帧 - How to specify x and y axis for plotting dataframe in Python 如何在 python 中绘图时设置 x 和 y 轴的值 - how to set value for x and y axis while plotting in python 使用matplotlib在熊猫中以对数比例绘制x和y轴 - plotting both x and y axis in log scale in pandas using matplotlib 使用 PCA 特征作为 X 轴和 Y 轴绘制簇 - Plotting clusters using PCA features as X an Y axis Python:从 x 轴上的点 (X1,0) 到点 (X2,Y2) 绘制线的问题 - Python: Problem with plotting lines from points (X1,0) on x axis to points (X2,Y2) 在 python3 中绘制散点图 plot,其中 x 轴是纬度/经度,单位为千米,y 轴是深度 - Plotting a scatter plot in python3 where x axis is latitude/longitude in km and y axis is depth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM