Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set x axis values in matplotlib python?

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?

like image 223
neha Avatar asked Jun 28 '17 22:06

neha


People also ask

How do you change the x-axis values in a scatter plot in Python?

To set axes of the scatter plot, use xlim() and ylim() functions. To plot the scatter graph, use scatter() function. To set label at the x-axis, use xlabel() function. To set label at y-axis, use ylabel() function.

How do you change the x-axis label in MatPlotLib?

MatPlotLib with Python Using subplot() method, add a subplot to the current figure. Plot x and log(x) using plot() method. Set the label on X-axis using set_label() method, with fontsize=16, loc=left, and color=red. To set the xlabel at the end of X-axis, use the coordinates, x and y.

How to set axis range in Matplotlib using Python?

Setting axis range in matplotlib using Python. We can limit the value of modified x-axis and y-axis by using two different functions:-. set_xlim () :- For modifying x-axis range. set_ylim () :- For modifying y-axis range.

How to set x-axis values in Matplotlib Python?

How to set X-axis values in Matplotlib Python? 1 Create two lists for x and y data points. 2 Get the xticks range value. 3 Plot a line using plot () method with xtick range value and y data points. 4 Replace xticks with X-axis value using xticks () method. 5 To display the figure, use show () method. More ...

How to add axes to a Matplotlib figure in Python?

Here, the first thing we have to do is to import two python module “ matplotlib ” and “ numpy ” by these line of codes :- Then we create a variable named “a” and set its value to plt.figure (). This creates a figure object, which is initially empty, because we haven’t inserted anything in it. Then add axes to this figure.

How do I set the X-limit in Pyplot?

Let's first set the X-limit, using both the PyPlot and Axes instances. Both of these methods accept a tuple - the left and right limits. So, for example, if we wanted to truncate the view to only show the data in the range of 25-50 on the X-axis, we'd use xlim ( [25, 50]):


1 Answers

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

like image 172
GWW Avatar answered Oct 05 '22 13:10

GWW