I want make a graph of a part of the values of a dictionary.
I already stored the necessary values in a variable, but I just don't understand how to put them in a simple graph with just the numbers 1 to 500 on the x-axis and my values on the y-axis.
%matplotlib inline
import matplotlib.pyplot as plt
# Plot frequencies of the most 500 words
frequencies = freqs_sorted[len(freqs_sorted)-500:len(freqs_sorted)]
Everything I tried so far resulted in an empty graph. Thanks in advance!
From the matplotlib tutorial:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.plot(range(1, 501), frequencies)
plt.show()
P.S. In matplotlib you often have several ways to draw whatever you want. It is just one of them. Here is shorter version from #gboffi:
plt.plot(frequencies)
plt.show()
Related
I am new to python and am trying to plot multiple lines in the same figure using matplotlib.
The value of my Y-axis is stored in a dictionary and I make corresponding values in X-axis in the following code
My code is like this:
for i in range(len(ID)):
AxisY= PlotPoints[ID[i]]
if len(AxisY)> 5:
AxisX= [len(AxisY)]
for i in range(1,len(AxisY)):
AxisX.append(AxisX[i-1]-1)
plt.plot(AxisX,AxisY)
plt.xlabel('Lead Time (in days)')
plt.ylabel('Proportation of Events Scheduled')
ax = plt.gca()
ax.invert_xaxis()
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
plt.show()
But I am getting separate figures with a single plot one by one. Can anybody help me figure out what is wrong with my code? Why can't I produce multiple-line plotting? Thanks a lot!
This is very simple to do:
import matplotlib.pyplot as plt
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.legend(loc='best')
plt.show()
You can keep adding plt.plot as many times as you like. As for line type, you need to first specify the color. So for blue, it's b. And for a normal line it's -. An example would be:
plt.plot(total_lengths, sort_times_heap, 'b-', label="Heap")
Since I don't have a high enough reputation to comment I'll answer liang question on Feb 20 at 10:01 as an answer to the original question.
In order for the for the line labels to show you need to add plt.legend to your code.
to build on the previous example above that also includes title, ylabel and xlabel:
import matplotlib.pyplot as plt
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.plot(<X AXIS VALUES HERE>, <Y AXIS VALUES HERE>, 'line type', label='label here')
plt.title('title')
plt.ylabel('ylabel')
plt.xlabel('xlabel')
plt.legend()
plt.show()
EDIT: I just realised after reading your question again, that i did not answer your question. You want to enter multiple lines in the same plot. However, I'll leave it be, because this served me very well multiple times. I hope you find usefull someday
I found this a while back when learning python
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
# create figure window
gs = gridspec.GridSpec(a, b)
# Creates grid 'gs' of a rows and b columns
ax = plt.subplot(gs[x, y])
# Adds subplot 'ax' in grid 'gs' at position [x,y]
ax.set_ylabel('Foo') #Add y-axis label 'Foo' to graph 'ax' (xlabel for x-axis)
fig.add_subplot(ax) #add 'ax' to figure
you can make different sizes in one figure as well, use slices in that case:
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0,:]) # row 0 (top) spans all(3) columns
consult the docs for more help and examples. This little bit i typed up for myself once, and is very much based/copied from the docs as well. Hope it helps... I remember it being a pain in the #$% to get acquainted with the slice notation for the different sized plots in one figure. After that i think it's very simple :)
If you work with Pandas it's very easy to do. For example:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [2, 2, 2]})
df.plot(kind='line')
See docs
I am currently creating a graph that that analyzes the correlation of absorption and concentration (Beer's law). While creating the graph, I've ran into a few problems, and I am now stuck. My plots are not showing up within my graph. Is it due to placement error? If possible, I would like to leave the ticks, labels, and title in the same (or similar format). Sorry in advance for the sloppiness, trying to get the function down before I make it pretty. But anyways, here is the code:
#importing matplotlib to create a graph
import matplotlib.pyplot as plt
#ploting out the points while labeling the graph
plt.plot([1.95E-06, 9.75E-06, 1.95E-05, 9.75E-05, 1.95E-04, 9.75E-04, 1.95E-
03],[0.2,0.4,0.6,0.8,1.0,1.2,1.4])
plt.xticks([1, 2, 3, 4, 5, 6, 7], [str('1.95E-03'), str('9.75E-04'),
str('1.95E-04'), str('9.75E-05'),str('1.95E-05'), str('9.75E-06'),
str('1.95E-06')])
plt.title('Red')
plt.ylabel('Absorption')
plt.xlabel('Concentration')
plt.grid(True)
plt.show()
Your xticks are completely out of the range where your data lives. Remove the line which sets the xticks and your plot is fine
import matplotlib.pyplot as plt
plt.plot([1.95E-06, 9.75E-06, 1.95E-05, 9.75E-05, 1.95E-04, 9.75E-04, 1.95E-03],
[0.2,0.4,0.6,0.8,1.0,1.2,1.4])
plt.title('Red')
plt.ylabel('Absorption')
plt.xlabel('Concentration')
plt.grid(True)
plt.show()
If you want to use your custom ticks, you need to set them in the data range, i.e. somewhere between 0 and 0.002 and not between 1 and 7.
Your data has x values well below 0.01, while your ticks start at 1, so your data will be to the left of the plot. I would suggest using a logarithmic x axis, just like the example below. This will also fix the problem with the x values being of different orders of magnitude. Note that I also put the tick strings in reverse order, assuming that you mistakenly wrote them the other way round. If not, please just go ahead and re-reverse them!
#importing matplotlib to create a graph
import matplotlib.pyplot as plt
x = [1.95e-06, 9.75e-06, 1.95e-05, 9.75e-05, 1.95e-04, 9.75e-04, 1.95e-03]
#ploting out the points while labeling the graph
plt.semilogx(x ,[0.2,0.4,0.6,0.8,1.0,1.2,1.4])
plt.xticks(x, [str('1.95E-03'), str('9.75E-04'), str('1.95E-04'), str('9.75E-05'),str('1.95E-05'), str('9.75E-06'), str('1.95E-06')], rotation=45)
plt.title('Red')
plt.ylabel('Absorption')
plt.xlabel('Concentration')
plt.grid(True)
plt.tight_layout()
plt.savefig('points.png')
plt.show()
The first argument to plt.xticks should be x-coords (not tick indexes).
I need to plot multiple sets of data on the same plot, and I use matplotlib.
For some of plots I use plt.plot() and for the others I use plt.errorbar(). But when I make a legend the ones created with plt.plot() appears first, no matter in which order I put them in the file (and zorder seems to have no effect on the position in the legend).
How can I give the order that I want in the legend, regardless of the way I plot the data?
You can adjust the order manually, by getting the legend handles and labels using ax.get_legend_handles_labels, and then reordering the resulting lists, and feeding them to ax.legend. Like so:
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots(1)
ax.plot(np.arange(5),np.arange(5),'bo-',label='plot1')
ax.errorbar(np.arange(5),np.arange(1,6),yerr=1,marker='s',color='g',label='errorbar')
ax.plot(np.arange(5),np.arange(2,7),'ro-',label='plot2')
handles,labels = ax.get_legend_handles_labels()
handles = [handles[0], handles[2], handles[1]]
labels = [labels[0], labels[2], labels[1]]
ax.legend(handles,labels,loc=2)
plt.show()
In the following code snippet:
import numpy as np
import pandas as pd
import pandas.rpy.common as com
import matplotlib.pyplot as plt
mtcars = com.load_data("mtcars")
df = mtcars.groupby(["cyl"]).apply(lambda x: pd.Series([x["cyl"].count(), np.mean(x["wt"])], index=["n", "wt"])).reset_index()
plt.plot(df["n"], range(len(df["cyl"])), "o")
plt.yticks(range(len(df["cyl"])), df["cyl"])
plt.show()
This code outputs the dot plot graph, but the result looks quite awful, since both the xticks and yticks don't have enough space, that it's quite difficult to notice both 4 and 8 of the cyl variable output its values in the graph.
So how can I plot it with enough space in advance, much like you can do it without any hassles in R/ggplot2?
For your information, both of this code and this doesn't work in my case. Anyone knows the reason? And do I have to bother to creating such subplots in the first place? Is it impossible to automatically adjust the ticks with response to the input values?
I can't quite tell what you're asking...
Are you asking why the ticks aren't automatically positioned or are you asking how to add "padding" around the inside edges of the plot?
If it's the former, it's because you've manually set the tick locations with yticks. This overrides the automatic tick locator.
If it's the latter, use ax.margins(some_percentage) (where some_percentage is between 0 and 1, e.g. 0.05 is 5%) to add "padding" to the data limits before they're autoscaled.
As an example of the latter, by default, the data limits can be autoscaled such that a point can lie on the boundaries of the plot. E.g.:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), 'ro')
plt.show()
If you want to avoid this, use ax.margins (or equivalently, plt.margins) to specify a percentage of padding to be added to the data limits before autoscaling takes place.
E.g.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10), 'ro')
ax.margins(0.04) # 4% padding, similar to R.
plt.show()
Can any one help me with this, i am new to graph a plot.
basically i have list of DocId's which are of some numbers and not necessarily in the order and this needs to come in X-Axies and timelst should come in Y-Axies.
below code actually plotting it, but DocId's not taken as respective DocID, its considered as range. so this needs to know and other thing is document list is huge may be i have 3000 - 5000 DocId's, can this graph goes for long for each DocID ?
import matplotlib.pyplot as plt
Doclst= [32409057,32409058,32409059,32409060,32409061,32409062,32409063,32409065,32409066,32409067]
timelst=[120,1,4,35,675,1240,500,889,99,10]
plt.plot(Doclst, timelst, marker='o', linestyle='--', color='r', label='time')
plt.xlabel('Document ID'+"'"+'s')
plt.ylabel('Time in Seconds')
plt.title('Performance')
plt.legend()
plt.savefig('graph.png')
Please help me as early as possible.
Update: I added two lines, one to access the axis properties, and the other one to force all the labels to show. To avoid the labels to overlap, I add also the argument rotate in plt.ticks to rotate the label by 30°.
If I get right what you want, you should simply use plt.xticks to customize tick labels. You can do something like that
import matplotlib as mpl
import matplotlib.pyplot as plt
Doclst= [32409057,32409058,32409059,32409060,32409061,32409062,32409063,32409065,32409066,32409067]
timelst=[120,1,4,35,675,1240,500,889,99,10]
fig, ax = plt.subplots()
plt.plot(Doclst, timelst, marker='o', linestyle='--', color='r', label='time')
plt.xlabel('Document ID'+"'"+'s')
plt.ylabel('Time in Seconds')
plt.title('Performance')
plt.legend()
plt.axis([Doclst[0], Doclst[-1], min(timelst)-1, max(timelst)+1])
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(len(Doclst)+1))
locs,label = plt.xticks()
plt.xticks(locs, map(lambda x: "%d" %x, locs), rotation=30)
plt.show()
which will give you something like that