Rageful
Rageful

Reputation: 13

Plotting a simple line between two points in Matplotlib

I want to draw a simple line as show in this figure.

Here is what I tried but failed:

import matplotlib.pyplot as plt
x = [1,10,100,1000,10000]
y = [1,10**(-1),10**(-2),10**(-3),10**(-4)] 
plt.plot(x, y,'gray',linestyle='--',marker='')

This code gives me very odd output. I need to make it same as in the figure.

I don't have all data points beside I only have those numbers in figure that I posted above.

Upvotes: 1

Views: 16795

Answers (1)

user3483203
user3483203

Reputation: 51165

Since you just want to plot 2 lines between 2 points, you only need 4 data points to plot this. However, to get the display you desire, there are several other tricks we need to apply here:

Plotting Data

x1 = [1, 100]
x2 = [1, 10**4]    
ys = [1, 10**-4]

The key is setting our axes to scale logarithmically, and setting our own labels for what should be displayed:

fig, ax = plt.subplots()
ax.plot(x1, ys, 'gray', linestyle=':', marker='')
ax.plot(x2, ys, 'black', linestyle='--', marker='')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xticks([1, 10, 100, 1000, 10000])
ax.set_yticks([10**-4, 10**-3, 10**-2, 10**-1, 10**0, 10**1])
ax.get_yaxis().get_major_formatter().labelOnlyBase = False
ax.get_xaxis().get_major_formatter().labelOnlyBase = False
plt.show()

Output:

enter image description here

Upvotes: 4

Related Questions