python matplotlib dash-dot-dot - how to? - python

I am using python and matplotlib to generate graphical output.
Is there a simple way to generate a dash-dot-dot line-style?
I am aware of the '--', '-.', and ':' options. Unfortunately, '-..' does not result in a dash-dot-dot line.
I have looked at the set_dashes command, but that seems to control the length of the dashes and the space between two adjacent dashes.
One option may be to plot two lines on top of each other; one dashed with ample space between the dashes - and one dotted, with the dots as large as the dashes are wide and spaced so that two dots are in between each of the dashes. I do not doubt this can be done, I am simply hoping for an easier way.
Did I overlook an option?

You can define custom dashes:
import matplotlib.pyplot as plt
line, = plt.plot([1,5,2,4], '-')
line.set_dashes([8, 4, 2, 4, 2, 4])
plt.show()
[8, 4, 2, 4, 2, 4] means
8 points on, (dash)
4 points off,
2 points on, (dot)
4 points off,
2 points on, (dot)
4 points off.
#Achim noted you can also specify the dashes parameter:
plt.plot([1,5,2,4], '-', dashes=[8, 4, 2, 4, 2, 4])
plt.show()
produces the same result shown above.

Related

Python matplotlib - setting x-axis scale

I have this graph displaying the following:
plt.plot(valueX, scoreList)
plt.xlabel("Score number") # Text for X-Axis
plt.ylabel("Score") # Text for Y-Axis
plt.title("Scores for the topic "+progressDisplay.topicName)
plt.show()
valueX = [1, 2, 3, 4] and
scoreList = [5, 0, 0, 2]
I want the scale to go up in 1's, no matter what values are in 'scoreList'. Currently get my x-axis going up in .5 instead of 1s.
How do I set it so it goes up only in 1?
Just set the xticks yourself.
plt.xticks([1,2,3,4])
or
plt.xticks(valueX)
Since the range functions happens to work with integers you could use that instead:
plt.xticks(range(1, 5))
Or be even more dynamic and calculate it from the data:
plt.xticks(range(min(valueX), max(valueX)+1))
Below is my favorite way to set the scale of axes:
plt.xlim(-0.02, 0.05)
plt.ylim(-0.04, 0.04)
Hey it looks like you need to set the x axis scale.
Try
matplotlib.axes.Axes.set_xscale(1, 'linear')
Here's the documentation for that function

Python version of h=area() in Matlab

I asked a related question yesterday and fortunately got my answer from jlarsch quickly. But now I am stuck with the next part, which starts with the h=area() line. I'd like to know the python version of the area() function, via which I will be able to set the colors. Could someone shed me some light again? Thanks much in advance.
...
Subplot (2,1,1);
H = plot (rand(100,5));
C = get (H, 'Color')
H = area (myX, myY);
H(1).FaceColor = C(1);
H(2).FaceColor = C(2);
Grid on;
...
The pretty much exact equivalent of MATLAB's Area plot is matplotlib's stackplot. Here is the first MATLAB example from the above link reproduced using matplotlib:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(4)
y = [[1, 3, 1, 2],
[5, 2, 5, 6],
[3, 7, 3, 1]]
plt.stackplot(x, y)
plt.show()
And here is the result:
You might be looking for pygame.draw.polygon(), which can fill a polygon defined by an arbitrary array of points.
You probably want plt.fill().
A huge amount of graph types at Matplotlib Gallery

How to generate image by using python and given data?

I have one data file which is like this:
1, 23%
2, 33%
3, 12%
I want to use python to generate one histogram to represent the percentage. I followed these command:
from PIL import Image
img = Image.new('RGB', (width, height))
img.putdata(my_data)
img.show()
However I got the error when I put the data: SystemError: new style getargs format but argument is not a tuple. Do I have to change my data file? and How?
A histogram is usually made in matplotlib by having a set of data points and then assigning them into bins. An example would be this:
import matplotlib.pyplot as plt
data = [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]
plt.hist(data, 7)
plt.show()
You already know what percentage of your data fits into each category (although, I might point out your percentages don't add to 100...). A way to represent this is to to make a list where each data value is represented a number of times equal to its percentage like below.
data = [1]*23 + [2]*33 + [3]*12
plt.hist(data, 3)
plt.show()
The second argument to hist() is the number of bins displayed, so this is likely the number you want to make it look pretty.
Documentation for hist() is found here:
http://matplotlib.org/api/pyplot_api.html
Are you graphing only? PIL is an image processing module - if you want histograms and other graphs you should consider matplotlib.
I found an example of a histogram here.

gnuplot.py - plotting time vs distance graph - multiple lines

How can I plot an x vs y graph in gnuplot.py? For example, this is what I want: http://www.mathwarehouse.com//graphs/distance-vs-time/images/distance-vs-time-graph-picture4.jpg
I want multiple lines.
My code:
import Gnuplot
g = Gnuplot.Gnuplot(debug=1)
g.title('A simple example') # (optional)
g('set data style linespoints') # give gnuplot an arbitrary command
g.title('Data can be computed by python or gnuplot')
g.xlabel('x')
g.ylabel('y')
one = ([0, 1], [2, 3], [5, 5])
g.plot(one)
raw_input()
Output: http://gyazo.com/ba9fb6d6762c864758a7b494d44d384f
Only has one line. If I try to plot another, nothing will happen.
Just add other data sets in you code
two=([6,4], [7,5], [8,3])
three=([0,2], [2,1], [5,6], [6,5], [7,7], [8,4])
and finally plot the three data sets by
g.plot(one, two, three)
Here is my output:
In the newer versions (>4.4?) of gnuplot, if you use:
g('set data style linespoints')
like in the original post, you'll get an error:
line 0: Unrecognized option. See 'help set'.
In stead, use:
g('set style data lines')

Pyplot set tick frequency and tick labels

I'm trying to make a plot with matplotlib where I want to specify both the position of the tick marks, and the text of the tick marks. I can individually do both with yticks(np.arange(0,1.1,1/16.)) and gca().set_yticklabels(['1','2','3']). However, for some reason when I do both of them together, the labels do not appear on the graph. Is there a reason for this? How can I get around it? Below is a working example of what I want to accomplish.
x = [-1, -0.2, -0.15, 0.15, 0.2, 7.8, 7.85, 8.15, 8.2, 12]
y = [1, 1, 15/16., 15/16., 1, 1, 15/16., 15/16., 1, 1]
figure(1)
plot(x,y)
xlabel('Time (years)')
ylabel('Brightness')
yticks(np.arange(0,1.1,1/16.))
xticks(np.arange(0,13,2))
ylim(12/16.,16.5/16.)
xlim(-1,12)
gca().set_yticklabels(['12/16', '13/16', '14/16', '15/16', '16/16'])
show(block = False)
Effectively I just wanted to replace the numerical values with fractions, but when I run this, the labels do not appear. It seems that using both yticks() and set_yticklabels together is a problem because if I remove either line, the remaining line works as it should.
If anyone can indicate how to simply force the label to be a fraction, that would also solve my problem.
EDIT:
I found an ugly workaround by using
ylim(12/16., 16.5/16)
gca().yaxis.set_major_locator(FixedLocator([12/16., 13/16., 14/16., 15/16., 16/16.]))
gca().yaxis.set_major_formatter(FixedFormatter(['12/16', '13/16', '14/16', '15/16', '16/16']))
While this may work for this specific example, it does not generalize well and it is cumbersome to specify the exact location and label of every tick mark. If anyone finds another solution, I'm all ears.
1) Your arange should produce 5 ticks, the same as labels you set.
arange is not good for that. It is better to use linspace.
2) You can set ticks and labels with the same function
plot(x,y)
xlabel('Time (years)')
ylabel('Brightness')
yticks(np.linspace(12/16., 1, 5), ('12/16', '13/16', '14/16', '15/16', '16/16') )
xticks(np.arange(0,13,2))
ylim(12/16.,16.5/16.)
xlim(-1,12)
3) Note that you should adjust the actual values of the axis with the position of the labels using linspace(12/16., 1, 5) instead of arange(0, 1.1, 1/16.))

Categories

Resources