Plotting values using matplotlib and find minimum by looking the graph - python-3.x

I have dictionary as:
```{'0.0': 2.445616223564293,
'0.05': 2.445594095119315,
'0.1': 2.4455740588234223,
'0.15': 2.4455560866270947,
'0.2': 2.4455401509059596,
'0.25': 2.4455262244535803,
'1.0': 2.4455411399961293,
'1.05': 2.44555597697399,
'1.1': 2.4455724183134344,
'1.15': 2.4455904432448716,
'1.2': 2.445610031303073,
'1.25': 2.4456311623222002,
'2.0': 2.4461204322901566,
'3.0': 2.447205696789686,
'4.0': 2.4486856713473726,
'5.0': 2.4504762863004363,
'10.0': 2.4623061878090624,
'20.0': 2.4922549001247876}```
Here all the values are different by some small factor. However when I plot it using matplotlib the plot is not distinctive.
I want to plot "keys" in x-axis and "values" in y-axis and then find x which has minimum y value by looking the plot.
I tried this code:
```plt.plot(*zip(*data))```
But the plot is not clear. How can I solve this problem such that plot is clearly able to show the difference in values.

The problem is your interpretation of zip(*data). I would suggest before plotting you first print and see what you are trying to plot.
print (list(zip(*data))) would print a list of splitted strings (keys of your data). To plot the keys on the x-axis and the values of the y-axis, simply do the following. I leave the visualization of the minimum up to you. If you want to plot the difference, subtract the first value from the complete list of values and then plot it on the y-axis.
plt.plot(data.keys(), data.values(), '-bx')
plt.xticks(rotation=45)

Related

Plotly does not color-code my chart according to the variable indicated

I have the problem that when I concatenate two csv and create a column "Version" with string values, plotly does NOT generate the classification of each of the values, in this case 1 and 2. However, when this variable is numeric, it does generate a continuous classification (I need a discrete classification).
Image shows concatenation and data type
Two legends are observed, but only one color is observed
This is an example of how it doesn't work. However, if I change the variable to continuous, it does work.
I am generating this block to generate graphs for each type of group. Which should be my final result.
grouped = df_final.groupby('Name')
plots = []
for name, group in grouped:
# create a new figure for the group
fig = px.scatter(group.reset_index(), x="Time", y="Observed", opacity=1, width=800, height=600,
color = "Version)
fig.show()
This block generates the chart with its legend, but does not show the colors in the chart.
I am starting with python and plotly, any help would be appreciated.
I'm trying to understand why plotly doesn't sort my string variables

Putting a value from an input data file into 'Set Label'

I'm plotting an animated surface in gnuplot and want to read in an average or sum of the mapped z values and include this in a label to be printed in the plot, so that I get a running total updated as the GIF progresses. It's probably straightforward, but I'm a "gnu"bie, so to speak, and find this system pretty confusing!
I've tried putting the running sum and average numbers in additional columns ...
splot 'output3.dat' index i:i using 1:2:(column(3), TD1 = strcol(4), TD2 = strcol(5)) with pm3d
but this doesn't plot, and the string variables TD1, TD2 don't seem to exist outside the splot command.
The command you show would indeed set variables TD1 and TD2 globally if you change the order of clauses in the serial evaluation expression (the comma-separated sub-expressions):
splot 'output3.dat' index i:i using 1:2:(TD1 = strcol(4), TD2 = strcol(5), column(3)) with pm3d
However, if the idea is to create a label using set label that will appear as part of the resulting graph, this won't work. The set label command would have to be executed before the splot command, so TD1 and TD2 will not have the correct values yet.
There is an alternative that might serve you better. Instead of trying to put this dynamically evaluate information in a label, put it in the plot title. Unlike a label, the plot title is evaluated after the corresponding plot is generated, so any variables set or updated by that plot will be current. [caveat: this is true for current gnuplot (version 5.4) but was not always true. If you have an older gnuplot version the title is evaluated before the plot rather than after].
Since current gnuplot also allows you to place the individual plot titles somewhere other than in the key proper, you have the same freedom that you would with a label to position the text anywhere on the output page. For example, if you want to sum the values in column 3 of the data file and print the total as part of a title above the resulting plot:
SUM = 0
splot 'foo.dat' using 1:2:(SUM = SUM+column(3), column(3)) with linespoints title 'foo.dat', \
keyentry title = sprintf("Points sum to %g", SUM) at screen 0.5 0.9
I used a separate keyentry clause because this allows to omit the sample line segment that would otherwise be generated, but it would also be possible to make this the title of the plot itself if you want that sample line.

Plot 3D Grid Data as Heat Map using gnuplot

Assume we have a 5x4x3 3D grid, where each cell has a value:
0.5523 0.0495 0.1465 0.5386
0.6299 0.4896 0.1891 0.6952
0.0320 0.1925 0.0427 0.4991
0.6147 0.1231 0.6352 0.5358
0.3624 0.2055 0.2819 0.4452
0.1239 0.2085 0.9479 0.6210
0.4904 0.5650 0.0821 0.5737
0.8530 0.6403 0.1057 0.0521
0.8739 0.4170 0.1420 0.9312
0.2703 0.2060 0.1665 0.7287
0.7378 0.8589 0.1339 0.3329
0.0634 0.7856 0.0309 0.4671
0.8604 0.5134 0.9391 0.6482
0.9344 0.1776 0.3013 0.0252
0.9844 0.3986 0.2955 0.8422
How can I achieve a plot in gnuplot that is similar to this plot achieved in matlab using patch? Note that the grid cells has an alpha value of 0.8.

gnuplot error: `Image grid must be at least 2 x 2'

I have a file with 5 columns of data in it. The first column are x indexes, the second are y indexes and the following three contain some data on which I perform some calculations in my gnuplot script as follows:
module(a,b,c) = sqrt(a**2+b**2+c**2);
splot 'myfile' using 1:2:(module($3,$4,$5)) with image
This works fine. However, if I try to plot the log10 of the module like this:
module(a,b,c) = log10(sqrt(a**2+b**2+c**2));
splot 'myfile' using 1:2:(module($3,$4,$5)) with image
it returns a blank plot and the warning of the title of the question.
I think it might be due to the zeros that are in my data, but even when I set an xy range to avoid those, the error persists.

How to prevent labels to overlap

I am running the following command to draw a few X,Y points in gnuplot:
plot "Output.tsv" using ($2+3):($3+3):1 with labels, "Output.tsv" using 2:3
Some of the data points are very close to each other and it makes the label unreadable. Is there a way to ask gnuplot to eliminate/reduce the overlap between labels?
I think you could consider 3 options:
1) make your graph huge and hope your labels do not overlap
2) plot the points as different series with each item having its own legend
3) use letters instead of labels, you can put a letter at each point using
plot "???" using 1:2
plot "" using 1:2:(stringcolumn(3) ne 'compare to' ? 'if equal' : 'if not equal' ) with labels
the stringcolumn function looks in column 3, compares the value to the string 'compareto' and if there is a match it puts 'if equal' at that location, otherwise 'if not equal'
Hence, I see something like Simulator in your graph, you could keep the green point and put an S with it/on it using
plot "" using 1:2:(stringcolumn(3) ne 'Simulator' ? 'S' : '' ) with labels
I hope this helps.

Resources