Python 在matplotlib中,我们是否可以将一行的某一列与同一行的另一列进行对比

Python 在matplotlib中,我们是否可以将一行的某一列与同一行的另一列进行对比,python,matplotlib,plot,Python,Matplotlib,Plot,我正在使用python绘制数据集。我希望一行的某一列与同一行的另一列相对应。准确地说,我希望我的两列是x轴和y轴,然后绘制用户输入的特定值,并绘制在该图形上 import matplotlib.pyplot as plt import pandas import numpy as np filename = 'friuts.csv' raw_data = open(filename, 'rb') data = pandas.read_csv(raw_data) mydata = pandas.

我正在使用python绘制数据集。我希望一行的某一列与同一行的另一列相对应。准确地说,我希望我的两列是x轴和y轴,然后绘制用户输入的特定值,并绘制在该图形上

import matplotlib.pyplot as plt
import pandas
import numpy as np

filename = 'friuts.csv'
raw_data = open(filename, 'rb')
data = pandas.read_csv(raw_data)
mydata = pandas.DataFrame(np.random.randn(10,2), columns=['col1','col2'])
mydata.hist()
plt.show()
我的数据集有两个不同的列,列中有水果名称和它们的权重。这两个权重可以作为x轴和y轴。但是,我一次只想要一个单行的图形。
我尝试的是获取所有行的整列

这就是你要找的吗


假设要使用给定行中的信息绘制单个点:

  • 使用水果名称选择行,这将返回pd.Series
  • 在结果上调用方法plot
  • 例如:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Create the data frame
    mydata = pd.DataFrame({
    'name': ['banana', 'mango', 'lima', 'apple'],
    'weight': [1, 2, 3, 4]})
    
    # Select the fruit you want to plot. This will return a pd.Series
    # including the colums 'name' and 'weight'
    to_plot = mydata[mydata['name'] == 'banana']
    
    # Call the plot function indicating the which column X and Y axis.
    fig, ax = plt.subplots()
    to_plot.plot(x='name', y='weight', marker='o', ax=ax)
    ax.set_ylabel('Weight')
    

    你能解释清楚你想要什么吗?你想要一张折线图吗?散点图?什么是单行“图”?你能试着重新制定你想要发生的事情吗?你是想把一列和另一列画出来吗?问题是,假设你有一种水果叫苹果,但不同的苹果根据质量的不同会有不同的重量。所以我希望权重在x轴上,在y轴上,其他一些值,然后我接受用户输入的任何权重。所以我希望用户输入的值被绘制在散点图中,以可视化权重是否在权重范围内。类似地,我在数据库中有不同的水果,用户将输入水果名称和重量,根据这一点,我的程序将获取特定的行并绘制散点图@mcpeterson@Mitch现在请看,我已经说明了我的问题,它将占据整个专栏。如果我只想要一个值呢。假设您有最小值和最大值,并在它们之间生成随机值,然后打印以向用户显示其值是否在范围内。我知道这可以用简单的if语句来实现,但我想可视化。
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Create the data frame
    mydata = pd.DataFrame({
    'name': ['banana', 'mango', 'lima', 'apple'],
    'weight': [1, 2, 3, 4]})
    
    # Select the fruit you want to plot. This will return a pd.Series
    # including the colums 'name' and 'weight'
    to_plot = mydata[mydata['name'] == 'banana']
    
    # Call the plot function indicating the which column X and Y axis.
    fig, ax = plt.subplots()
    to_plot.plot(x='name', y='weight', marker='o', ax=ax)
    ax.set_ylabel('Weight')