38

Graph

I want to draw this graph using matplotlib. I wrote the code but it's not changing the x axis values.

import matplotlib.pyplot as plt
x = [0.00001,0.001,0.01,0.1,0.5,1,5]
y = [0.945,0.885,0.893,0.9,0.996,1.25,1.19]
plt.xlim(0.00001,5)
plt.ylim(0.8,1.4)
plt.plot(x, y, marker='o', linestyle='--', color='r', 
label='Square') 
plt.xlabel('x')
plt.ylabel('y') 
plt.title('compare')
plt.legend() 
plt.show()

How I can draw the blue line of the given graph using matplotlib?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
neha
  • 1,858
  • 5
  • 21
  • 35

1 Answers1

64

The scaling on your example figure is a bit strange but you can force it by plotting the index of each x-value and then setting the ticks to the data points:

import matplotlib.pyplot as plt
x = [0.00001,0.001,0.01,0.1,0.5,1,5]
# create an index for each tick position
xi = list(range(len(x)))
y = [0.945,0.885,0.893,0.9,0.996,1.25,1.19]
plt.ylim(0.8,1.4)
# plot the index for the x-values
plt.plot(xi, y, marker='o', linestyle='--', color='r', label='Square') 
plt.xlabel('x')
plt.ylabel('y') 
plt.xticks(xi, x)
plt.title('compare')
plt.legend() 
plt.show()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
GWW
  • 43,129
  • 11
  • 115
  • 108