简体   繁体   English

plt.scatter() 图的行为类似于 Matplotlib 中的 plt.plot() 图

[英]plt.scatter() plots behaving like plt.plot() plots in Matplotlib

I'm trying to compare the GDP-per-capita of the world's countries' to each countries' COVID-19 death total.我试图将世界各国的人均 GDP 与每个国家的 COVID-19 死亡总数进行比较。 Every time I try to turn it into a scatter plot, it displays the same plot as would be displayed using the plt.plot() command.每次我尝试将其变成散点图 plot 时,它显示的 plot 与使用plt.plot()命令显示的相同。 Here is my code:这是我的代码:

import pandas as pd
from matplotlib import pyplot as plt
plt.style.use('seaborn-whitegrid')
data = pd.read_csv(r'/Users/john.smith/covid-data.csv')

gdp = data["gdp_per_capita"]
deaths = data["total_deaths"]

plt.scatter(gdp, deaths)
plt.title('GDP-per-Capita Compared to COVID-19 Death Total')
plt.xlabel('GDP-per-Capita')
plt.ylabel('Confirmed Deaths')

plt.tight_layout()
plt.show()

While running this code, the following graph is produced.运行此代码时,会生成以下图表。 This is obviously not the scatter plot I'm trying to get, and it's worth noting that the only thing that changes when I use the plt.scatter() command is that the points on the plot just get very large.这显然不是我想要得到的分散 plot,值得注意的是,当我使用plt.scatter()命令时唯一改变的是 plot 上的点变得非常大。

使用错误代码生成的图表

I ran a test of the whole Matplotlib module entirely on a different file.我完全在不同的文件上对整个 Matplotlib 模块进行了测试。 When I use normal variables without importing from a CSV file, like this:当我使用普通变量而不从 CSV 文件导入时,如下所示:

x = [7, 3, 8, 3]
y = [1, 5, 7, 4]
plt.scatter(x, y)

Then the code works perfectly fine and produces a scatter plot.然后代码工作得很好,并产生了一个分散的 plot。 I have been digging for hours online to try and find a solution, and have tried to use other methods of importing CSVs or creating scatter plots but nothing is working.我一直在网上挖掘几个小时试图找到解决方案,并尝试使用其他导入 CSV 或创建散点图的方法,但没有任何效果。 Thank you for any tips.谢谢你的任何提示。

Answer is courtesy of G. Anderson in the comments above.答案由上述评论中的G. Anderson提供。

As it turns out I just didn't have experience with the xlim() and ylim() commands, so the individual points in the scatter plot just overlapped very tightly in vertical lines.事实证明,我只是没有使用xlim()ylim()命令的经验,因此散点图 plot 中的各个点在垂直线上非常紧密地重叠。 The reason this happened is simply because the original view window was too wide for this large of a dataset.发生这种情况的原因仅仅是因为原始视图 window 对于这么大的数据集来说太宽了。

I did some slight additional research to try and put two plots onto a single figure with one being zoomed in, here's the code:我做了一些额外的研究,尝试将两个图放在一个图形上,其中一个被放大,这是代码:

figs, axs = plt.subplots(2)
figs.suptitle('GDP-per-Capita Compared to COVID-19 Death Total')
axs[0].scatter(gdp, deaths)
axs[1].scatter(gdp, deaths)
plt.axis([10000, 20000, 10000, 20000])

This produced some nice plots I can use:这产生了一些我可以使用的好图:

在此处输入图像描述

I'm going to look into ways to make the two plots much more readable.我将研究使这两个图更具可读性的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM