Home > OS >  Python plot_data
Python plot_data

Time:11-17

It first generates some data in a csv file format and the plots it using the plot_data method.

How to solve the bug?

import csv
import matplotlib.pyplot as plt

f = open("data_file.csv", "w")
w = csv.writer(f)
_ = w.writerow(["precision", "recall"])
w.writerows([[0.013,0.951],
             [0.376,0.851],
             [0.441,0.839],
             [0.570,0.758],
             [0.635,0.674],
             [0.721,0.604],
             [0.837,0.531],
             [0.860,0.453],
             [0.962,0.348],
             [0.982,0.273],
             [1.0,0.0]])
f.close()

matplotlib.pyplot.show('data_file.csv')

I am a student, learning Python, how should I plot the data? thank you

CodePudding user response:

You can try this also to display plot

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open('data_file.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter = ',')
    
    for row in plots:
        x.append(row[0])
        y.append(row[1])

plt.bar(x, y, color = 'g', width = 0.72, label = "recall")
plt.xlabel('precision')
plt.ylabel('recall')
plt.title('Title')
plt.legend()
plt.show()

or

import matplotlib.pyplot as plt
import csv

x = []
y = []

with open('data_file.csv','r') as csvfile:
    lines = csv.reader(csvfile, delimiter=',')
    for row in lines:
        x.append(row[0])
        y.append(row[1])

plt.plot(x, y, color = 'g', linestyle = 'dashed',
        marker = 'o',label = "precision")

plt.xticks(rotation = 25)
plt.xlabel('precision')
plt.ylabel('recall')
plt.title('Title', fontsize = 20)
plt.grid()
plt.legend()
plt.show()

CodePudding user response:

So the thing is you have to call plt.plot() before with the data, and then call plt.show() You could do something like:

import csv
import matplotlib.pyplot as plt

f = open("data_file.csv", "w")
w = csv.writer(f)
_ = w.writerow(["precision", "recall"])
rows = [[0.013,0.951],[0.376,0.851],[0.441,0.839],[0.570,0.758],[0.635,0.674],[0.721,0.604],[0.837,0.531],[0.860,0.453],[0.962,0.348],[0.982,0.273],[1.0,0.0]]
precision = [row[0] for row in rows]
recall = [row[1] for row in rows]
w.writerows(rows)
f.close()
plt.plot(precision, recall)
plt.show()

Above you have your data in rows, and your two variables divided in precision and recall. So now you can call plt.plot(precision, recall) and then plt.show

CodePudding user response:

Ideally, your code should have thrown a NameError at the last line, saying 'matplotlib' is not defined. Since you have already imported matplotlib.pyplot as plt, use plt as a reference later in the code, instead of calling matplotlib.pyplot again and again.

You cannot pass a CSV file directly into the plt.show() function as an argument.

I suggest you use Pandas package to create a dataframe out of your CSV file and plot the values like in the below code:

import csv
import matplotlib.pyplot as plt
import pandas as pd

f = open("data_file.csv", "w")
w = csv.writer(f)
_ = w.writerow(["precision", "recall"])
w.writerows([[0.013,0.951],
             [0.376,0.851],
             [0.441,0.839],
             [0.570,0.758],
             [0.635,0.674],
             [0.721,0.604],
             [0.837,0.531],
             [0.860,0.453],
             [0.962,0.348],
             [0.982,0.273],
             [1.0,0.0]])
f.close()

Dataframe = pd.read_csv('data_file.csv')

plt.plot(Dataframe.precision, Dataframe.recall)
plt.show()

You can check more about Pandas documentation here and matplot documentation here

  • Related