I'm trying to draw line chart from my data that are read from excel. Here is what I have tried till now.
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.svm import OneClassSVM
from sklearn.cluster import DBSCAN
from rtree.index import Rtree
import math
from decimal import Decimal
y1=[]
y2=[]
xls = pd.ExcelFile('/Users/Downloads/excel-data.xlsx')
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')
#sheet1
x_df1 = df1[["percentage"]].to_numpy()
y_df1 = df1[["precision"]].to_numpy()
#sheet2
x_df2 = df2[["percentage"]].to_numpy()
y_df2 = df2[["precision"]].to_numpy()
#print(x_df2)
#converting to 1D
x_flatten = x_df1.reshape(-1)
y_flatten = y_df1.reshape(-1)
x_flatten_df2 = x_df2.reshape(-1)
y_flatten_df2 = y_df2.reshape(-1)
#print(x_flatten_df2)
for ele in y_flatten:
y1.append("{:.3f}".format(float(ele)))
for ele1 in y_flatten_df2:
y2.append("{:.3f}".format(float(ele1)))
# Plot a simple line chart
plt.plot(x_flatten,y1,'c',label='Line y')
#print(x_flatten)
#print(y1)
plt.plot(x_flatten,y2,'y',label='Line z')
# Plot another line on the same chart/graph
#plt.plot(x, z)
plt.legend()
plt.show()
here is what it shows me
However I need my graph to be something like the following having less numbers in the vertical axis. But having more numbers in vertical axis rather than the following picture so it can show the exact value for each point of the line better.
Here is my data from sheet1: precision[0.9076,0.8960,0.88125,0.86171,0.83405,0.79302,0.725161,0.71341,0.72360] percentage[10,20,30,40,50,60,70,80,90]
Also I can I add points on my line charts like the above chart? I appreciate of any help.
CodePudding user response:
I have changed and hopefully simplified your code. From what I understood, you want to have the y-axis go from 0-1 instead of it being what is in the figure and need the markers to show each point. See if this works
import pandas as pd
import matplotlib.pyplot as plt
df1=pd.read_excel('input.xlsx', 'Sheet1', index_col=0) #Read df1 with percentage as index
df2=pd.read_excel('input.xlsx', 'Sheet2', index_col=0) #Read df2 with percentage as index
fig, ax = plt.subplots() #Using ax so that both plots are drawn together and use same axis
df1.plot(ax=ax, marker = 'o') #Draw df1 plot with marker
df2.plot(ax=ax, marker = 'o') #Draw df2 plot with marker
ax.legend(["Line y", "Line z"]) #Show legend with labels as you need
ax.set_ylim([0,1]) # Set y-axis limits from 0 to 1
ax.grid(axis='y') # Show horizontal grid lines
Plot