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

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')

Related

Plotly: How to adjust size of markers in a scatter geo map so that differences become more visible?

I used library Plotly in Python: chart: Bubble-Map to display average_score of countries. However, average_score has values between 2-4 and therefore the size of the bubbles in the bubble-map chart does not differentiate much (the size of bubbles is very similar). How can I achieve a bigger difference among bubbles with values 2-4?
Here is my piece of code:
plot = px.scatter_geo(df, locations="country_code", color="country_code", size_max=20,hover_name="country_code", size="avg_score", animation_frame="year", projection="natural earth",title="Bubble Map",labels={"country_code": "Country"})
I would raise your raw data to the power of a number that suits the visualization you're aiming to build. This makes larger numbers look disproportionally larger than sall numbers. Compare the two plots below where the first is the px.scatter_geo() example from the docs, and the second where the same data for population df['pop'] has been replaced with df['pop']**1.6.
1. Raw data for df['pop']
1. Data for df['pop'] raised to the power of 1.6
Of course these numbers have no other business in the figure, so you will have to include the following in order to keep the correct hoverinfo:
fig.update_traces(hovertemplate = 'pop=%{text}<br>iso_alpha=%{location}<extra></extra>', text = df['pop'])
Complete code:
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
df['pop_display'] = df['pop']**1.6
fig = px.scatter_geo(df, locations="iso_alpha",
size="pop_display",
)
fig.update_traces(hovertemplate = 'pop=%{text}<br>iso_alpha=%{location}<extra></extra>', text = df['pop'])
fig.show()
What you're asking is not really a Plotly question but a general math question.
Given the inputs [2,3,4] step=1. return corresponding integers that have step > 1. There are multiple ways you can accomplish this:
One way is to multiply all items by an integer.
[2,3,4] * 2 = [4, 6, 8] # step=2
[2,3,4] * 3 = [6, 9, 12] # step=3
...
In this case the difference between new values will rise linearly. Meaning that the step between all values will remain constant.
If you want step to grow in a non linear way you can square the items:
[2,3,4]^2 = [4, 9, 16] # step=5, 7...
[2,3,4]^3= [8, 27, 64] # step=19,37...
...
Possibilities are really endless. It all depends on what kind of difference you want between the bubbles. In code, quick and dirty solution will look something like this:
plot = px.scatter_geo(df,
locations="country_code",
color="country_code",
size_max=20,
hover_name="country_code",
size=df["avg_score"]**2,
animation_frame="year",
projection="natural earth",
title="Bubble Map",
labels={"country_code": "Country"}
)

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

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.

Plot missing points for complicated 3D list of points - Python

Hi I have a 3D list (I realise this may not be the best representation of my data so any advice here is appreciated) as such:
y_data = [
[[a,0],[b,1],[c,None],[d,6],[e,7]],
[[a,5],[b,2],[c,1],[d,None],[e,1]],
[[a,3],[b,None],[c,4],[d,9],[e,None]],
]
The y-axis data is such that each sublist is a list of values for one hour. The hours are the x-axis data. Each sublist of this has the following format:
[label,value]
So essentially:
line a is [0,5,3] on the y-axis
line b is [1,2,None] on the y-axis etc.
My x-data is:
x_data = [0,1,2,3,4]
Now when I plot this list directly i.e.
for i in range(0,5):
ax.plot(x_data, [row[i][1] for row in y_data], label=y_data[0][i][0])
I get a line graph however where the value is None the point is not drawn and the line not connected.
What I would like to do is to have a graph which will plot my data in it's current format, but ignore missing points and draw a line between the point before the missing data and the point after (i.e. interpolating the missing point).
I tried doing it like this https://stackoverflow.com/a/14399830/1800665 but I couldn't work out how to do this for a 3D list.
Thanks for any help!
The general approach that you linked to will work fine here ; it looks like the question you're asking is how to apply that approach to your data. I'd like to suggest that by factoring out the data you're plotting, you'll see more clearly how to do it.
import numpy as np
y_data = [
[[a,0],[b,1],[c,None],[d,6],[e,7]],
[[a,5],[b,2],[c,1],[d,None],[e,1]],
[[a,3],[b,None],[c,4],[d,9],[e,None]],
]
x_data = [0, 1, 2, 3, 4]
for i in range(5):
xv = []
yv = []
for j, v in enumerate(row[i][1] for row in y_data):
if v is not None:
xv.append(j)
yv.append(v)
ax.plot(xv, yv, label=y_data[0][i][0])
Here instead of using a mask like in the linked question/answer, I've explicitly built up the lists of valid data points that are to be plotted.

python matplotlib dash-dot-dot - how to?

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.

Categories

Resources