-2

I'm really new to python but I made a program which produced data I want on a line graph. I know how to use bar graphs, but I haven't found any method (I can understand or can use without throwing an error) to make a line graph.

Here is an example me trying to plot some data:

import matplotlib.pyplot as plt
x = []
y = []
width = 0.5
for i in range(100):
    x.append(i)
    y.append(i)
_ = plt.xlabel('Number of moves')
_ = plt.ylabel('Frequnecy')
_ = plt.bar(x, y, color='r', width=width)
plt.show()   

All I want to know is how to convert data like the one in the for loop into a line graph. Try to think of the simplest way of doing it because I'm not very smart. An explanation attached to the solution would be greatly appreciated.

Cheers, Evan.

EDIT: Thanks to an answer-er, I solved the problem. I would also like to know how to add a title to a graph, and also I want to know how to increase the length of the line of the x-axis (the data of my graph is kind of squished).

Evan Kilroy
  • 1
  • 1
  • 4
  • What do you mean by "increase the length of my x-axis"? Make the plotted figure larger? Present it in a different aspect ratio? Define the start and end point of the axis? I suggest reading also [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [some matplotlib tutorials](https://matplotlib.org/tutorials/index.html). – Mr. T Sep 09 '18 at 06:35
  • I can't believe when you say **I haven't found any method ... to make a line graph**. A simple search for **line plot python** will give you over 100 examples. – Sheldore Sep 09 '18 at 09:36

3 Answers3

0

Use plt.plot instead of plt.bar to get a line graph

_ = plt.plot(x, y, color='r')
plt.show()
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • Thanks a lot! A couple questions: Why wouldn't it let me add the width argument? Are there any parameters I can get to spread out the data (i.e increase the length of my x-axis)? – Evan Kilroy Sep 09 '18 at 05:12
0

Use plt.plot to use a line graph and plt.title to add an title to your graph.

plt.title("this is a title")
plt.show()
pgmcr
  • 79
  • 7
0

I would do suggest you read over this tutorial in the link below

https://matplotlib.org/gallery/lines_bars_and_markers/simple_plot.html

and for the line width adjustment you just need to pass in linewidth parameter into the plot function as

plt.plot(x, y, linewidth=2.0)

If I am not wrong, also you were wondering adjusting axis limits on your plot. That can be done by plt.xlim(5, 0). For more please check

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xlim.html

Hope that helps!

Ozkan Serttas
  • 947
  • 13
  • 14