`I am trying to reproduce the attached figure step by step. My problem was that how can i plot colorbar in above figure by my data. My data is a cosmological data and it has 7 columns totally with many raw. My main goal is reproducing the present figure step by step. You can see that there are three different plots which are interpolated each other. Firstly, i tried to plot small colorful lines in the body of figure by using two columns of data. I did it by scatter plots and then i needed to reproduce the colorbar part of figure. But, it was not possible at the first attempt. Because, the colorbar points was not a part of data. Then, i obtained the values of colorbar by some calculations and added them as additional columns to data. Now, i could you the simple colorbar function to do colorbar part. And i got it. For the next step, i need to turn small curved lines to dark solid lines.
How can I do plots in matplotlib?
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
data1 = np.loadtxt("bei_predic.txt", unpack=True)
B = np.log10(data1[3]/(4.*(data1[2])))
R = np.vstack((data1,B))
R = np.transpose(R)
D = R[~np.isnan(R).any(axis=1)]
A = plt.scatter(D[:,3],D[:,2], c=D[:,8])
cbar= plt.colorbar()
cbar.set_label("file", labelpad=+1)
plt.show()
If you could start off by telling us a little bit about the data that you are using that would be great. In order to plot the figure that you want, we must first load the data into some variables. Have you managed to do this?
Check out this example in which the author plots multicolored lines for some guidance.
Related
I have functions that produces plots,
And i used it three time with different values to give me three plots.
My question is that i want to put the plots side by side horizontally to be able to compare them.
As doing the following show every plot after the other vertically.
make_plot(twiss)
make_plot(twiss_error)
make_plot(twiss_corrected)
If you are using matplotlib perhaps use subplots:
https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.subplots.html
import matplotlib.pyplot as plt
fig, ax = plt.subplots (1,2)
you can access subplots by index as in
ax[0]
in your code.
Hope that this helps.
I am trying to use matplotlib to show some data in a clear way. My current goal is to label the data using two methods: color and shape. The color will be used to represent the data set these specific points come from, while the shape is used to represent whether that example is in category one or two. To visualize this, here is a simple example I drew in PowerPoint:
The reason for doing this instead of simply creating a legend with each specific data set and category stated is I am plotting upwards of 10 data sets, so the legend would remain significantly cleaner and easier to read if color was used for the data sets and shape used for general category (thus the legend would show 10 colors and two shapes, as opposed to 20 different color-shape combinations).
I am currently able to use matplotlib to set the label of the individual data sets by iterating through them and plotting each individually as follows:
import matplotlib.pyplot as plt
ax = plt.figure()
for data in datasets:
scat_plot = ax.scatter(data[x], data[y], label=data[label])
ax.legend()
plt.show()
However, when I attempt to plot the individual shapes and colors and assign them the same label, I am left with plots that do not recognize the two scatter collections as having the same label.
Any suggestions or hints would be greatly appreciated. Thank you.
I am trying to get two different plots as one plot. I will not write down my entire code (is so long), but based on the two small codes below, i get two different time series and I want to put these together in one figure.
My code for the first plot:
plt.figure(figsize=(15,4))
i = plt.plot(july/july.mean(),label='G')
my code for my second plot:
spi3 = pd.read_csv('SPI3.csv',header=0,parse_dates=True)
spi3.plot(y='spi',figsize=(16,4))
Quick dirty fix would be to plot dictionaries at first, only then plot with plt.plot. Also, if you want to plot in the same figure, define figsize only in the first figure you are plotting. (Therefore plt.figure is ommitted completely.)
spi3.plot(y='spi',figsize=(16,4))
plt.plot(july/july.mean(),label='G')
EDIT - I was being stupid, and trying to plot strings. I converted to int and plotted again fine. Thanks to ImportanceOfBeingErnest for the hint.
I have data from 3 sensors which I want to plot, using matplotlib
Each array is of different length, and I plot them using the following line of code
plt.plot(s_1,'r',s_3,'b',s_4,'g')
plt.show()
This produces the following graph
As you can see, the green trace is not correct, and the y-axis scale is off (these is a 6 after the 21).
I'm really not sure what the problem is here.
When I plot the data individually, they are fine:
It is the last one in this series that is plotted strangely in the graph with all three at once.
To be clear, I don't understand why separately the graphs plot fine, but when the three are printed in one plot the y-axis gets messed up.
Any advice around what the issue with the three-in-one plot is would be great.
I am using Jupyter-notebook with python 3.6.2 and matplotlib to plot some data.
When I plot my data, I want to add a legend to the plot (basically to know which line is which)
However calling plt.legend takes a lot of time (almost as much as the plot itself, which to my understanding should just be instant).
Minimal toy problem that reproduces the issue:
import numpy as np
import matplotlib.pyplot as plt
# Toy useless data (one milion x 4)
my_data = np.random.rand(1000000,4)
plt.plot(my_data)
#plt.legend(['A','C','G','T'])
plt.show()
The data here is just random and useless, but it reproduces my problem:
If I uncomment the plt.legend line, the run takes almost double the time
Why? Shouldn't the legend just look at the plot, see that 4 plots have been made, and draw a box assigning each color to the corresponding string?
Why is a simple legend taking so much time?
Am I missing something?
Replicating the answer by #bnaecker, such that this question is answered:
By default, the legend will be placed in the "best" location, which requires computing how many points from each line are inside a potential legend box. If there are many points, this can take a while. Drawing is much faster when specifying a location other than "best", e.g. plt.legend(loc=3).